Search code examples
pythonimagepython-requestsurllib

Saving an image from a URL that does not end with image extension


I'm a python beginner. I have a dataset column that contains thousands of URLs. I want to save the image in each URL with its extension. I don't have a problem with urls that end with the image extension like https://web.archive.org/web/20170628093753im_/http://politicot.com/wp-content/uploads/2016/12/Sean-Spicer.jpg.(with urllib or requests) However for URLs like link1= https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200 or link2 = https://i2.wp.com/www.huzlers.com/wp-content/uploads/2017/03/maxresdefault.jpeg?fit=1280%2C720&ssl=1, i failed to save them. I want to save the images in links as follows: image1.jpg and image2.jpeg. How can we do this? Any help could be useful.


Solution

  • The following seems to work for me, give it a try:

    import requests
    urls = ['https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200',
            'https://i2.wp.com/www.huzlers.com/wp-content/uploads/2017/03/maxresdefault.jpeg?fit=1280%2C720&ssl=1']
    
    for i, url in enumerate(urls):
        r = requests.get(url)
        filename = 'image{0}.jpg'.format(i+1)
    
        with open(filename, 'wb') as f:
            f.write(r.content)