gpt4 book ai didi

Python, 基础题 : How do I download multiple url's with urllib. request.urlretrieve

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:10 25 4
gpt4 key购买 nike

我有以下功能齐全的工作代码:

import urllib.request
import zipfile

url = "http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2sop"
filename = "C:/test/archive.zip"
destinationPath = "C:/test"

urllib.request.urlretrieve(url,filename)
sourceZip = zipfile.ZipFile(filename, 'r')

for name in sourceZip.namelist():
sourceZip.extract(name, destinationPath)
sourceZip.close()

它会完美运行几次,但因为我从中检索文件的服务器有一些限制,一旦达到每日限制,我就会收到此错误:

Traceback (most recent call last):
File "script.py", line 11, in <module>
urllib.request.urlretrieve(url,filename)
File "C:\Python32\lib\urllib\request.py", line 150, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "C:\Python32\lib\urllib\request.py", line 1591, in retrieve
block = fp.read(bs)
ValueError: read of closed file

如何更改脚本,使其包含多个 url 的列表,而不是一个 url,并且脚本一直尝试从列表中下载直到成功,然后继续解压缩。我只需要一次成功下载。

很抱歉我是 Python 的新手,但我无法理解这一点。我假设我必须将变量更改为如下所示:

url = {
"http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2soe",
"http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2sod",
"http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2soc",
"http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2sob",
"http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2soa",
}

然后将这一行更改为某种循环:

urllib.request.urlretrieve(url,filename)

最佳答案

您想将您的网址放在一个列表中,然后遍历该列表并尝试每一个。您捕获但忽略它们抛出的异常,并在成功后中断循环。试试这个:

import urllib.request
import zipfile

urls = ["http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2sop", "other url", "another url"]
filename = "C:/test/test.zip"
destinationPath = "C:/test"

for url in urls:
try:
urllib.request.urlretrieve(url,filename)
sourceZip = zipfile.ZipFile(filename, 'r')
break
except ValueError:
pass

for name in sourceZip.namelist():
sourceZip.extract(name, destinationPath)
sourceZip.close()

关于Python, 基础题 : How do I download multiple url's with urllib. request.urlretrieve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7016252/

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