How to download image using requests
You can either use the response.raw
file object, or iterate over the response.
To use the response.raw
file-like object will not, by default, decode compressed responses (with GZIP or deflate). You can force it to decompress for you anyway by setting the decode_content
attribute to True
(requests
sets it to False
to control decoding itself). You can then use shutil.copyfileobj()
to have Python stream the data to a file object:
import requests
import shutil
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
To iterate over the response use a loop; iterating like this ensures that data is decompressed by this stage:
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r:
f.write(chunk)
This'll read the data in 128 byte chunks; if you feel another chunk size works better, use the Response.iter_content()
method with a custom chunk size:
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
Note that you need to open the destination file in binary mode to ensure python doesn't try and translate newlines for you. We also set stream=True
so that requests
doesn't download the whole image into memory first.
recommended way to download images in python requests
I love the minimalist way. There is nothing called right way. It depends on the task you want to perform and the constraints you have.
import requests
with open('file.png', 'wb') as f:
f.write(requests.get(url).content)
# if you change png to jpg, there will be no error
Download image with python requests
First, look at the content of the response with response.text
, you'll see the website blocked your request.
Please turn JavaScript on and reload the page.
Then, you can try to check if changing the User-Agent of your request fixes your issue.
response = requests.get(
url,
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
},
stream = True
)
If it doesn't, you may need to get your data with something which can parse javascript like selenium or Puppeteer.
Download image using requests that contains a query string
The problem is with the filename. You need to first split by ?
then take the first element then split by /
import requests # to get image from the web
import shutil # to save it locally
## Set up the image URL and filename
image_url = "https://instagram.fybz2-1.fna.fbcdn.net/v/t51.2885-15/fr/e15/p1080x1080/106602453_613520712600632_6255422472318530180_n.jpg?_nc_ht=instagram.fybz2-1.fna.fbcdn.net&_nc_cat=108&_nc_ohc=WQizf6rhDmQAX883HrQ&oh=140f221889178fd03bf654cf18a9d9a2&oe=5F4D2AFE"
filename = image_url.split("?")[0].split("/")[-1]
# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream=True)
# Check if the image was retrieved successfully
if r.status_code == 200:
# Set decode_content value to True, otherwise the downloaded image file's size will be zero.
r.raw.decode_content = True
# Open a local file with wb ( write binary ) permission.
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
print('Image sucessfully Downloaded: ', filename)
else:
print('Image Couldn\'t be retreived')
unable to download image using request in python url send me html in respons?
Actually you were getting response code 429 which is assigned to Too Many Requests
with description of The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes
But in your case it can be Bypassed via User-Agent
to be in Headers
, you don't need to use stream=True
as well since it's already an open stream url.
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}
r = requests.get(
"https://i8.amplience.net/i/nlyscandinavia/086399-0001_01/i-light-rib-polo-top/", headers=headers)
with open("photo.jpg", 'wb') as f:
f.write(r.content)
How to save or download an image that I get in a request -- Python
Try this:
import requests
Picture_request = requests.get(Photo_URL)
if Picture_request.status_code == 200:
with open("/path/to/image.jpg", 'wb') as f:
f.write(Picture_request.content)
Related Topics
How to Pad a String With Zeroes
Tkinter - Executing Functions Over Time
Convert Hex String to Integer in Python
Changing Default Encoding of Python
Convert Python Dict into a Dataframe
Django Csrf Check Failing With an Ajax Post Request
Dynamically Set Local Variable
How to Format a Floating Number to Fixed Width in Python
How to Have One Colorbar For All Subplots
How to Sort a List of Objects Based on an Attribute of the Objects
How to Call a Function Within a Class
What Is the Result of % in Python
How Are Python'S Built in Dictionaries Implemented