Load multiple images with incremental urls

Question:

i have 1 problem i would like everyone to help. I need to download image files on 1 website, links https://acb.xyz.0001.jpg and the following files it similarly only change the number of extensions to https://acb.xyz.0002.jpg. Is there a way to use python to save those images. I tried to manually save each photo but it was too much.

Asked By: TBFTTH

||

Answers:

Assuming you have already know how many image you need to download you can use this code.

import requests
# Url to download content from 
URL = "http://thuvien.hmu.edu.vn/pages/cms/TempDir/books/01419632-d344-4a8e-9f53-4e40be647c3a/2022/09/13/202209071405-e0309899-377b-43d1-9033-972aa96844b5/FullPreview/"
# suffix 000001.jpg
# :05d
# Maximum number of image to download
max_image_size =3

# function for writing requests to png files
def dumpfile(n, m):
    f = open(n, 'wb')
    f.write(m)
    f.close()

# Itearate over the image urls and call dumpfile function
for i in range(1,max_image_size):
    suffix = str(f'{i:06d}')
    target=URL+ suffix+'.jpg'# added number of png
    print(target)
    response = requests.get(target)
    
    fileName=str(i)+'.jpg'
    dumpfile(fileName,response.content)



Answered By: NsaNinja
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.