L
L
Loyxim2020-08-30 20:36:28
Python
Loyxim, 2020-08-30 20:36:28

Error while downloading a picture using urllib, what should I do?

I'm trying to download a picture using urllib.request (urlretrieve), I get an error (access to the picture is not denied):

Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 5, in <module>
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 222, in urlopen    return opener.open(url, data, timeout)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 640, in http_response
    response = self.parent.error(
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

The code:
import urllib.request
urllib.request.urlretrieve('url', 'Test.png')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-08-30
@hottabxp

403 - forbidden. It just won't work to download the image. Most likely it is necessary to transfer the correct headings. If you need authorization to download, then you need to log in to the site and transfer the necessary cookies. Or log in to the browser, copy the cookies and pass them in the script.
Also, instead of urllib in the 21st century, it is customary to use requests.

S
soremix, 2020-08-30
@SoreMix

access to the image is not prohibited

403: Forbidden

hm.
Log in to the site, or pass the necessary headers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question