2013-01-08 74 views
1

我正在尋找更快的方式來完成我的任務。我有40000個文件可下載的網址。我想將它們下載到本地桌面上。現在我的想法是將鏈接放在瀏覽器上,然後通過腳本下載它們。現在我正在尋找的是將10個URL放在一個塊中地址欄並同時獲得10個要下載的文件。如果有可能希望整體時間會減少。can python可用於在一次性瀏覽器中打開多個選項卡?

對不起,我來晚了給的代碼,那就是:

def _download_file(url, filename): 
    """ 
    Given a URL and a filename, this method will save a file locally to the» 
    destination_directory path. 
    """ 
    if not os.path.exists(destination_directory): 
     print 'Directory [%s] does not exist, Creating directory...' % destination_directory 
     os.makedirs(destination_directory) 
    try: 
     urllib.urlretrieve(url, os.path.join(destination_directory, filename)) 
     print 'Downloading File [%s]' % (filename) 
    except: 
     print 'Error Downloading File [%s]' % (filename) 


def _download_all(main_url): 
    """ 
    Given a URL list, this method will download each file in the destination 
    directory. 
    """ 

    url_list = _create_url_list(main_url) 
    for url in url_list: 
     _download_file(url, _get_file_name(url)) 

感謝,

+0

我已經給出了代碼,希望它能讓你清楚明白我擁有什麼以及我在找什麼! –

+0

請重新打開這個問題,這是一個有效的問題! –

+2

爲什麼使用瀏覽器?這看起來像[XY問題](http://meta.stackexchange.com/a/66378)。要下載文件,我會使用類似[requests](http://docs.python-requests.org/)的庫(或者對wget進行系統調用);例如,通過「導入請求」下載文件; 'r = requests.get('http://imgs.xkcd.com/comics/tech_support_cheat_sheet.png')'; '用file('new_image.png','w')作爲f:f.write(r.content)';每個分號應該是一個換行符(儘管在註釋中不能寫),它下載一個圖像並將其寫入'new_image.png'文件。 –

回答

2

爲什麼要使用一個瀏覽器?這看起來像是一個XY problem

要下載文件,我會使用庫如requests(或撥打wget的系統調用)。

事情是這樣的:

import requests 

def download_file_from_url(url, file_save_path): 
    r = requests.get(url) 
    if r.ok: # checks if the download succeeded 
     with file(file_save_path, 'w') as f: 
      f.write(r.content) 
     return True 
    else: 
     return r.status_code 

download_file_from_url('http://imgs.xkcd.com/comics/tech_support_cheat_sheet.png', 'new_image.png') 
# will download image and save to current directory as 'new_image.png' 

首先必須使用你喜歡的任何Python包管理器例如,pip install requests安裝請求。你也可以更有魅力;例如,

相關問題