2

test.txt には、ダウンロードするファイルのリストが含まれています。

http://example.com/example/afaf1.tif
http://example.com/example/afaf2.tif
http://example.com/example/afaf3.tif
http://example.com/example/afaf4.tif
http://example.com/example/afaf5.tif

これらのファイルは、Python を使用して最大のダウンロード速度でどのようにダウンロードできますか?

私の考えは次のとおりでした。

import urllib.request
with open ('test.txt', 'r') as f:
    lines = f.read().splitlines()
    for line in lines:
        response = urllib.request.urlopen(line)

その後は?ダウンロード先の選択方法は?

4

2 に答える 2

1

これは私にとってはうまくいきます:(名前は絶対でなければならないことに注意してください、例えば「afaf1.tif」)

import urllib,os
def download(baseUrl,fileName,layer=0):
    print 'Trying to download file:',fileName
    url = baseUrl+fileName
    name = os.path.join('foldertodwonload',fileName)
    try:
        #Note that folder needs to exist
        urllib.urlretrieve (url,name)
    except:
        # Upon failure to download retries total 5 times
        print 'Download failed'
        print 'Could not download file:',fileName
        if layer > 4:
            return
        else:
            layer+=1
        print 'retrying',str(layer)+'/5'
        download(baseUrl,fileName,layer)
    print fileName+' downloaded'

for fileName in nameList:
    download(url,fileName)

try ブロックから不要なコードを移動しました

于 2013-09-18T13:19:26.630 に答える