pythonimagedownload

can't download image with python


try to download images with python but only this picture can't download it
i don't know the reason cause when i run it, it just stop just nothing happen no image , no error code ...

here's the code plz tell me the reason and solution plz..

import urllib.request

num=404

def down(URL):

    fullname=str(num)+"jpg"
    urllib.request.urlretrieve(URL,fullname)
    im="https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg"

down(im)

Solution

  • this code will work for you try to change the url that you use and see result :

    import requests
    
    pic_url = "https://www.thesun.co.uk/wp-content/uploads/2020/09/67d4aff1-ddd0-4036-a111-3c87ddc0387e.jpg"
    cookies = dict(BCPermissionLevel='PERSONAL')
    
    
    with open('aa.jpg', 'wb') as handle:
            response = requests.get(pic_url, headers={"User-Agent": "Mozilla/5.0"}, cookies=cookies,stream=True)
            if not response.ok:
                print (response)
    
            for block in response.iter_content(1024):
                if not block:
                    break
    
                handle.write(block)
    

    enter image description here