Python 下载pdf循环
我试图使用Python3.5从同一个站点下载几个pdf,但是我只下载了第一个pdf,它进入了一个循环 任何帮助都将不胜感激Python 下载pdf循环,python,pdf,Python,Pdf,我试图使用Python3.5从同一个站点下载几个pdf,但是我只下载了第一个pdf,它进入了一个循环 任何帮助都将不胜感激 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/med
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")
导入urllib.request
导入urllib.error
first=int(输入('first:'))
last=int(输入('last:'))
如果首先使用while
而不是if
如果
只检查一次条件并下载文件,则它是一个分支运算符<代码>而
是循环运算符
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")
导入urllib.request
导入urllib.error
first=int(输入('first:'))
last=int(输入('last:'))
首先,你说“它进入一个循环”是什么意思?正如Namit的回答所示,代码中没有循环,但应该有一个循环。您是否知道如何解决“urllib.error.HTTPError:HTTP error 404:Not Found”?@CarlosLozano这意味着您获取的url不正确,并且在服务器网站上找不到。是的,某些文件不可用。我正在检查urllib的文档,但我不确定如何处理它。您可以在while循环中添加一个try-except块,它捕获urllib.error.HTTPError,如果页面不可用,请忽略它。