0

(Sorry for bad English) I'm trying to get an image from https://thiscatdoesnotexist.com/ , but none of the solutions didn't work, because I couldn't find any url referring to JPG file. Tried to use this code, which expectably failed:

import requests

url = 'https://thiscatdoesnotexist.com/thiscatdoesnotexist.jpg'

img_data = requests.get(url).content
with open('image_name.jpg', 'wb') as handler:
    handler.write(img_data)

got an error:

Traceback (most recent call last):
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 1336, in getresponse
    response.begin()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 275, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\util\retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\BodyPozitive\home\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 1336, in getresponse
    response.begin()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "c:\users\bodypozitive\appdata\local\programs\python\python37-32\Lib\http\client.py", line 275, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "aaa.py", line 8, in <module>
    img_data = requests.get(url).content
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\BodyPozitive\home\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Are you sure the url is correct? Check the status code. You could also take a look at [this](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests) – Grebtsew Oct 25 '19 at 15:58
  • Oh, url is really incorrect, I was just playing around with it, and forget to delete 'thiscatdoesnotexist.jpg' part. An error appears even if I call this code on root URL – KalYmGg 3 Oct 25 '19 at 16:07
  • 1
    The problem is that they check your user agent and deny any requests that look automated (they don't want you scraping them). You need to fake a user agent (I just tried and it works). – Carcigenicate Oct 25 '19 at 16:12
  • Not sure if you've already finished, but I had some fun writing up a simple bulk scraper. [Here](https://gist.github.com/carcigenicate/5db85e90c519eef30fa51b1a07279cb5) if you want it. – Carcigenicate Oct 25 '19 at 16:49

0 Answers0