python save image from url

前端 未结 9 1208
滥情空心
滥情空心 2020-12-04 09:41

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

9条回答
  •  误落风尘
    2020-12-04 10:19

    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)
    

提交回复
热议问题