【发布时间】:2021-08-06 09:27:12
【问题描述】:
我有一个作为下载对话框打开的文件的 URL 列表,其中包含保存和打开选项。 我正在使用 python 请求模块来下载文件。在使用 Python IDLE 时,我可以使用以下代码下载文件。
link = fileurl
r = requests.get(link,allow_redirects=True)
with open ("a.torrent",'wb') as code:
code.write(r.content)
但是当我将此代码与 for 循环一起使用时,下载的文件已损坏或无法打开。
for link in links:
name = str(links.index(link)) ++ ".torrent"
r = requests.get(link,allow_redirects=True)
with open (name,'wb') as code:
code.write(r.content)
【问题讨论】:
-
在写入文件之前检查请求的状态。
-
我认为这与您正在使用的其他组件有关。您能否更清楚/完整地描述一下这段代码 sn-p 周围的环境?
-
因为你一次又一次地重写同一个文件
-
@rdas 我得到的状态码是 200
-
@NikolayPatarov 我试过写差异文件名。但它没有帮助
标签: python for-loop download python-requests torrent