Download pdf loop

user6088719

I'm trying to download several pdfs from the same site using a python 3.5 but I get to download just the first pdf and it goes into a loop.

Any help would be highly appreciated.

import urllib.request
import urllib.error

first = int(input('First:'))
last = int(input('Last:'))

if first <= last:
    response = urllib.request.urlopen("http://www.netapp.com/us/media/tr-" + str(first) +".pdf")
    file = open(str(first) + ".pdf", 'wb')
    file.write(response.read())
    file.close()
    response.close()
    first = first + 1
else:
    print("Completed")
Namit Singal

Use a while instead of an if. if will only check for the condition once, and download the file, it is a branching operator. while is a looping operator.

import urllib.request
import urllib.error

first = int(input('First:'))
last = int(input('Last:'))

while first <= last:
    response = urllib.request.urlopen("http://www.netapp.com/us/media/tr-" + str(first) +".pdf")
    file = open(str(first) + ".pdf", 'wb')
    file.write(response.read())
    file.close()
    response.close()
    first = first + 1
print("Completed")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related