简体   繁体   中英

Not able to download all the images using request python

I have 1k of image urls in a csv file and I am trying to download all the images from the urls. I don't know why I am not able to download all the images. Here is my code:

print('Beginning file download with requests')
path = '/home/tt/image_scrap/image2'
for idx, url in tqdm(enumerate(dataset['url']), total=len(dataset['url'])):
    response = requests.get(url,stream=True)
    time.sleep(2)

    filename = url.split("/")[-1]
    with open(path+'/'+filename, 'wb') as f:
        f.write(response.content)

Try / Except statements are really good for these type of 'errors': Try this:

try:
    with open(path+'/'+filename, 'wb') as f:
    f.write(response.content)
except Exception as error:
    print(error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM