Home > OS >  Download consecutive Images Python
Download consecutive Images Python

Time:08-07

newbie here.. so i want to:

  • download images which have consecutive names;
  • input the link so it will works with every website that has the same syntax;
  • input how many pages i have to download;
  • have it works with .png and jpgs.

    At first I made a simple Python requests code with 40 variables, 20 for png and 20 for jpg, so it would work with 20 pages, but then i had to modify for 60 pages so 120 variables.. it works but i want to make it capable to download how many pages i want without adding more and more variables.

Example of link: www.website.com/path/1.png ... /2.png ... /3.jpg ... /4.png ...

This is what i made so far,

import requests
import shutil

for i in list(range(1,92)): 
    link = input("insert link:") 
    name = str(i) ".jpg" 
    r = requests.get(link name, stream=True)
    if r.status_code == 200:
            with open(link name ".png", 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

My code now, thanks for the very usefull answer!

import requests
import shutil

linkFormat = input("Link without the /1.png but with {num}:") 
cap = input("number cap") #it's useful to sort images
endRange = int(input("Last page:"))

for i in range(1, endRange): #it always starts for 1, less input
    singleLink = linkFormat.format(num = str(i))
    r = requests.get(singleLink ".png", stream=True)
    rj = requests.get(singleLink ".jpg", stream=True)
    if r.status_code == 200:
        with open(cap   "_"   str(i)   ".png" , 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
            print("Downloading: "   str(i))
    if rj.status_code == 200:
        with open(cap   "_"   str(i)   ".jpg", 'wb') as f:
            rj.raw.decode_content = True
            shutil.copyfileobj(rj.raw, f)
            print("Downloading: "   str(i))

CodePudding user response:

# example link:  www.website.com/path/{num}.png
linkFormat = input("insert link:") 
startRange = int(input("first number:"))
endRange = int(input("first number:"))

for i in range(startRange, endRange): 
    singleLink = linkFormat.format(num = str(i))
    print(singleLink)
    r = requests.get(singleLink, stream=True)
    if r.status_code == 200:
            with open(singleLink, 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

You can use string.format to insert a number at an arbitrary position in a format string, specified by the format. Try something like what is above. Here is some documentation:

https://www.w3schools.com/python/ref_string_format.asp

This solution means that any link that has any kind of consecutive number can be used. So the link can have any format you like. Here are some sample outputs:

insert link:hi{num}.com
first number:0
first number:10
hi0.com
hi1.com
hi2.com
hi3.com
hi4.com
hi5.com
hi6.com
hi7.com
hi8.com
hi9.com
insert link:www.example.com/images/space_image_{num}/128x128.png
first number:5
first number:9
www.example.com/images/space_image_5/128x128.png
www.example.com/images/space_image_6/128x128.png
www.example.com/images/space_image_7/128x128.png
www.example.com/images/space_image_8/128x128.png
  • Related