How to download this specific img using python?
Question:
You can open this link and you will see the image on your browser, you can right click on it and save it but you won’t be able to do it through a Python script, what am I missing here?
I tried this simple approach and also with selenium both approaches fail, if you use the script below it will create a file with 155 bytes that is definitely not the jpg:
import requests
url = 'https://icodrops.com/wp-content/uploads/2022/10/Ai_sYzYL_400x400-150x150.jpg'
response = requests.get(url)
with open("1.jpg", 'wb') as f:
f.write(response.content)
Answers:
Adding the headers
works for me:
import requests
url = 'https://icodrops.com/wp-content/uploads/2022/10/Ai_sYzYL_400x400-150x150.jpg'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=headers)
with open("1.jpg", 'wb') as f:
f.write(response.content)
Keep in mind that the image will be generated where you run your Python script (the CWD).
You can open this link and you will see the image on your browser, you can right click on it and save it but you won’t be able to do it through a Python script, what am I missing here?
I tried this simple approach and also with selenium both approaches fail, if you use the script below it will create a file with 155 bytes that is definitely not the jpg:
import requests
url = 'https://icodrops.com/wp-content/uploads/2022/10/Ai_sYzYL_400x400-150x150.jpg'
response = requests.get(url)
with open("1.jpg", 'wb') as f:
f.write(response.content)
Adding the headers
works for me:
import requests
url = 'https://icodrops.com/wp-content/uploads/2022/10/Ai_sYzYL_400x400-150x150.jpg'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=headers)
with open("1.jpg", 'wb') as f:
f.write(response.content)
Keep in mind that the image will be generated where you run your Python script (the CWD).