Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载pdf循环_Python_Pdf - Fatal编程技术网

Python 下载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

我试图使用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/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,如果页面不可用,请忽略它。