2013-02-15 169 views
0

我需要從網頁下載適合的文件。我使用-i選項的wget這樣做:我下載的文件存儲在其中包含URL1,URL2一個LIST.TXT文件...然後使用wget下載文件

$ wget -i list.txt 

你知道,如果有做的可能性同樣的事情使用python腳本? 謝謝。

回答

1

假設你的文件包含每行一個網址,你可以這樣做:

import urllib2 
with open('list.txt') as my_list: 
    for line in my_list: 
     response = urllib2.urlopen(line) 
     html = response.read() 
     # now process the page's source 
+0

好吧,似乎它的工作原理。但是如果網站受到用戶名和密碼的保護呢? 這種情況下的相同腳本返回 urllib2.HTTP錯誤:HTTP錯誤401:需要授權 我該如何解決問題? – user2044983 2013-02-15 14:15:29

+0

請參閱http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly上的示例 – mbatchkarov 2013-02-15 14:23:08

0
with open('list.txt') as my_list: 
    for url in my_list: 
     wget.download(url)