gpt4 book ai didi

python - 下载 pdf 循环

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:22 32 4
gpt4 key购买 nike

我正在尝试使用 python 3.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")

最佳答案

使用while而不是ifif只会检查一次条件,并下载文件,它是一个分支运算符。 while 是一个循环运算符。

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")

关于python - 下载 pdf 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110936/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com