2011-08-10 19 views
3

で複数のURLのをダウンロードしない私は、次の完全に機能し、動作するコードがあります。Pythonの、基本的な質問:どのように私はurllib.request.urlretrieve

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() 

をそれが動作する数回を完璧ますが、理由は、サーバI私は、毎日の限界に達すると、いくつかの制限があるからファイルを取得しています、私はこのエラーが表示されます。それは、複数のURLのリストを含むように

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 

はどのようにして、代わりに1つのURLに、スクリプトを変更しませんスクリプトは成功するまでリストからダウンロードしようとし続けます解凍して私はちょうど1つの成功したダウンロードが必要です

非常に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) 

答えて

2

あなたはあなたのURLをリストに入れて、そのリストをループしてそれぞれを試してみたいです。あなたが捕捉しても例外を無視し、ループが成功するとループを破ります。これを試してみてください:

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() 
+0

これは私が欲しかったものでした。ありがとうございました。 – Amivit

0

あなたはRetry-decoratorで見ることができCelery-retry

またはチェックアウトCeleryとその再試行メカニズム、 例:

import time 

# Retry decorator with exponential backoff 
def retry(tries, delay=3, backoff=2): 
    """Retries a function or method until it returns True. 

    delay sets the initial delay, and backoff sets how much the delay should 
    lengthen after each failure. backoff must be greater than 1, or else it 
    isn't really a backoff. tries must be at least 0, and delay greater than 
    0.""" 

    if backoff <= 1: 
    raise ValueError("backoff must be greater than 1") 

    tries = math.floor(tries) 
    if tries < 0: 
    raise ValueError("tries must be 0 or greater") 

    if delay <= 0: 
    raise ValueError("delay must be greater than 0") 

    def deco_retry(f): 
    def f_retry(*args, **kwargs): 
     mtries, mdelay = tries, delay # make mutable 

     rv = f(*args, **kwargs) # first attempt 
     while mtries > 0: 
     if rv == True: # Done on success 
      return True 

     mtries -= 1  # consume an attempt 
     time.sleep(mdelay) # wait... 
     mdelay *= backoff # make future wait longer 

     rv = f(*args, **kwargs) # Try again 

     return False # Ran out of tries :-(

    return f_retry # true decorator -> decorated function 
    return deco_retry # @retry(arg[, ...]) -> true decorator 
0
urls = [ 
"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", 
] 

for u in urls: 
    urllib.request.urlretrieve(u,filename) 
    ... rest of code ... 
1
import urllib.request 
import zipfile 

urllist = ("http://url.com/archive.zip?key=7UCxcuCzFpYeu7tz18JgGZFAAgXQ2sop", 
      "another", 
      "yet another", 
      "etc") 

filename = "C:/test/test.zip" 
destinationPath = "C:/test" 

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

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

これは、あなただけの停止、その後、1つの作品まで、一度各それらを試してみたいと仮定していきます。