如何使用Python3通过HTTPS下载图像

时间:2017-06-15 11:27:56

标签: python-3.x https

我已经看过关于这个问题的各种答案,但似乎都没有。

我有url,它直接链接到我想下载的图片。

url = https://content3.cdnprado.net/imagenes/Documentos/imgsem/a3/a331/a331fb54-a6d4-4732-8509-daf462aa92bb/dd5afc00-81d5-4e84-aca4-5377a4722391.jpg

我的尝试:

Using urllib.request.urlretrieve()
    urllib.request.urlretrieve(url,os.getcwd())
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 248, in urlretrieve
        with contextlib.closing(urlopen(url, data)) as fp:
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
        return opener.open(url, data, timeout)
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
        response = meth(req, response)
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
        'http', request, response, code, msg, hdrs)
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
        return self._call_chain(*args)
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
        result = func(*args)
      File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
        raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 403: Forbidden

urlib.request.urlopen()

with urllib.request.urlopen(url) as d, open('test.jpg','wb') as  file:
    data = d.read()
    file.write(data)
    Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

使用请求

import requests
response = requests.get(url,verify=False, auth=('user', 'pass'))
with open('file.jpg','w') as fout:
    fout.write(response.read())

C:\Users\mjpvanzuijlen\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
Traceback (most recent call last):
  File "<input>", line 4, in <module>
AttributeError: 'Response' object has no attribute 'read'

几乎所有相关问题都只适用于HTTP,或者根本不适用于此网址。

0 个答案:

没有答案