使用请求python下载文件



我有一个作为下载对话框打开的文件的Url列表,其中包含保存和打开选项。我正在使用python请求模块来下载文件。在使用PythonIDLE时,我可以下载带有以下代码的文件。

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)

如果您试图从网站下载视频,请尝试

r = get(video_link, stream=True)
with open ("a.mp4",'wb') as code:
for chunk in r.iter_content(chunk_size=1024):
file.write(chunk)                

最新更新