2017-04-16 4 views
-1

내가 그래서 (random.cat하는 요청을 전송함으로써) URL을 검색 : 인터넷에서 파일을 읽으시겠습니까?

print('Importing REQUESTS') 
import requests 
import json 
import urllib 
response = (requests.get("http://random.cat/meow")) 
response = str(response.content) 
print(response) 
response = response.replace("b'","") 
response = response.replace("'","") 
response = response.replace("\\","") 
print(response) 
data = json.loads(response) 
print (data["file"])` 

그럼 내가 사용을 열려고은 :

:

with open(line, 'rb') as f: 
    print("work")` 

나는이 오류

Traceback (most recent call last): 
    File "G:/catimages.py", line 21, in <module> 
    with open(line, 'rb') as f: 
OSError: [Errno 22] Invalid argument: 'http://random.cat/i/8Vilp.jpg' ` 

아이디어가 있으십니까?

답변

0

open URL을 직접 열 수 없습니다. 먼저 파일을 다운로드하기 위해 요청을 사용해야합니다. How do I download a file over HTTP using Python?

편집을 참조하십시오 : 명확하게하기 위해, 사용자가 제공 한 오류 메시지가 line의 값이 'http://random.cat/i/8Vilp.jpg' 있음을 나타냅니다, 그래서 당신은 작동하지 않습니다 open('http://random.cat/i/8Vilp.jpg', 'rb')를 호출하려고합니다.