This download image from the URL example will show you how to use the python urllib module, requests module, and wget module to download an image file from an image URL. You will find this example code is simple and clear. Below example code can also be used to download any web resources with a URL.
1. Use Python urllib Module To Implement Download Image From URL Example.
- Enter python interactive console by running
python
orpython3
command in a terminal, then run the below source code in it.>>> import urllib.request >>> >>> image_url = "https://www.dev2qa.com/demo/images/green_button.jpg" >>> >>> urllib.request.urlretrieve(image_url, 'local_image.jpg') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(*args) File "/Users/songzhao/opt/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Users/songzhao/opt/anaconda3/lib/python3.7/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
- But you will find above source code will throw an error message urllib.error.HTTPError: HTTP Error 403: Forbidden. This is because the web server rejects the HTTPS request sent from urllib.request.urlretrieve() method which does not provide a User-Agent header ( The urllib.request.urlretrieve() method does not send a User-Agent header to the webserver ).
- So we should change the above source code to the below which can send the User-Agent header to the webserver then the HTTPS request can be completed successfully.
>>> from urllib.request import urlopen, Request >>> # Simulate a User-Agent header value. >>> headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'} >>> >>> image_url = "https://www.dev2qa.com/demo/images/green_button.jpg" >>> >>> local_file = open('local_image_1.jpg','wb') >>> # Send request with simulated User-Agent header. >>> req = Request(url=image_url, headers=headers) >>> >>> with urlopen(req) as response: ... local_file.write(response.read()) ... 5486
2. Use Python requests Module To Implement Download Image From URL Example.
- Open a terminal, and run command
python
orpython3
to enter the python interactive command console. - Run below example code in the above python interactive command console. The example image URL is https://www.dev2qa.com/demo/images/green_button.jpg. After running the below python code, it will download the image and save it to a local file local_image.jpg.
# Import python requests, shutil module. import requests import shutil # This is the image url. image_url = "https://www.dev2qa.com/demo/images/green_button.jpg" # Open the url image, set stream to True, this will return the stream content. resp = requests.get(image_url, stream=True) # Open a local file with wb ( write binary ) permission. local_file = open('local_image.jpg', 'wb') # Set decode_content value to True, otherwise the downloaded image file's size will be zero. resp.raw.decode_content = True # Copy the response stream raw data to local image file. shutil.copyfileobj(resp.raw, local_file) # Remove the image url response object. del resp
- Above python source code use both the python requests and shutil module. But you can use the python requests module only to implement it also like below source code.
>>> import requests >>> >>> local_file = open('local_file.png','wb') >>> >>> image_url = "https://www.dev2qa.com/demo/images/green_button.jpg" >>> >>> resp = requests.get(image_url, stream=True) >>> >>> local_file.write(resp.content) 5486 >>> local_file.close()
3. Use Python Wget Module To Implement Download Image From URL Example.
- Besides the python requests module, the python wget module can also be used to download images from URL to local file easily. Below are the steps about how to use it.
- Open a terminal and run
pip show wget
to check whether the python wget module has been installed or not. - If the python wget module has not been installed, then run the command
pip install wget
in the terminal to install it.$ pip install wget Collecting wget Downloading https://files.pythonhosted.org/packages/47/6a/62e288da7bcda82b935ff0c6cfe542970f04e29c756b0e147251b2fb251f/wget-3.2.zip Installing collected packages: wget Running setup.py install for wget ... done Successfully installed wget-3.2
- Run
python
orpython3
to enter the python interactive command console. - Run below python code in the above python interactive command console.
# First import python wget module. >>> import wget >>> image_url = 'https://www.dev2qa.com/demo/images/green_button.jpg' # Invoke wget download method to download specified url image. >>> local_image_filename = wget.download(image_url) 100% [................................................] 829882 / 829882> # Print out local image file name. >> local_image_filename 'green_button.jpg'
References
I tried using the first approach (changed nothing) and it produced empty image (0kb)
second approach produced error: HTTPError: Forbidden
Do you know why is that? I am using python 3.7 and spyder IDE
A bit late, It means you don’t have access to see that resource
For the HTTPError: Forbidden error, it is because do not provide a User-Agent header when sending the request. The article has been updated, it has include how to fix this error.