-1

When I retrieve a URL (By sending a request to random.cat) as so:

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"])`

Then I attempt to open it using:

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

I get this error:

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' `

Any ideas?

m0nhawk
  • 22,980
  • 9
  • 45
  • 73

1 Answers1

0

open can't directly open URL's. You'll need to use requests to download the file first. See How do I download a file over HTTP using Python?

Edit: To be clear, the error message you provided indicates that the value of line is 'http://random.cat/i/8Vilp.jpg', so you are attempting to call open('http://random.cat/i/8Vilp.jpg', 'rb') which will not work.

Community
  • 1
  • 1
Flight Odyssey
  • 2,267
  • 18
  • 25