I got a problem when I am using python to save an image from url either by urllib2 request or urllib.urlretrieve. That is the url of the image is valid. I could download it
For linux in case; you can use wget command
import os
url1 = 'YOUR_URL_WHATEVER'
os.system('wget {}'.format(url1))
Anyone who is wondering how to get the image extension then you can try split method of string on image url:
str_arr = str(img_url).split('.')
img_ext = '.' + str_arr[3] #www.bigbasket.com/patanjali-atta.jpg (jpg is after 3rd dot so)
img_data = requests.get(img_url).content
with open(img_name + img_ext, 'wb') as handler:
handler.write(img_data)
Python3
import urllib.request
print('Beginning file download with urllib2...')
url = 'https://akm-img-a-in.tosshub.com/sites/btmt/images/stories/modi_instagram_660_020320092717.jpg'
urllib.request.urlretrieve(url, 'modiji.jpg')
Output result
import random
import urllib.request
def download_image(url):
name = random.randrange(1,100)
fullname = str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_image("http://site.meishij.net/r/58/25/3568808/a3568808_142682562777944.jpg")
import urllib.request
import os
img_url = "https://betanews.com/wp-content/uploads/2017/09/firefox-logo.jpg"
img_name = os.path.basename(img_url)
urllib.request.urlretrieve(img_url,img_name)
import requests
img_data = requests.get(image_url).content
with open('image_name.jpg', 'wb') as handler:
handler.write(img_data)