使用循环下载 urls/files 的列表 - python

Downloading list of urls/files using loop - python

我需要下载大约 1000 个file/url,手动下载会很困难。

我试图将 url 放入列表中并循环遍历列表,但我认为我的代码覆盖了以前的文件并仅保留列表中的最后一项

这是我的代码

#!/usr/bin/env python

import urllib3
http = urllib3.PoolManager()

urls = ["http://url1.nt.gz" , "http://url2.nt.gz" , "http://url3.nt.gz"]
N =1; // counter helps me to rename the downloaded files
print "downloading with urllib"
for url in urls
 r = http.request('GET',url)
 Name =str(N+1) // each time increment the counter by one 
 with open("file"+Name+".nt.gz", "wb") as fcont:
                fcont.write(r.data)

有什么建议吗?

您没有递增计数器 - 您加 1 而没有将其保存回 N

设置Name后添加N += 1。您在 for.

之后缺少 :

我不太确定你的 1000 多个 url 在哪里 - 我在 urls 中只看到 3 个。

#!/usr/bin/env python

import urllib3
http = urllib3.PoolManager()

urls = ["http://url1.nt.gz" , "http://url2.nt.gz" , "http://url3.nt.gz"]
N =1; // counter helps me to rename the downloaded files
print "downloading with urllib"
for url in urls:
    r = http.request('GET',url)
    Name =str(N+1) 
    N += 1
    with open("file"+Name+".nt.gz", "wb") as fcont:
        fcont.write(r.data)

print "downloading with urllib" for url in urls r = http.request('GET',url) Name += N