使用wget下载文件
问题描述:
我需要从网页下载适合的文件。我使用-i选项的wget这样做:我下载的文件存储在其中包含URL1,URL2一个LIST.TXT文件...然后使用wget下载文件
$ wget -i list.txt
你知道,如果有做的可能性同样的事情使用python脚本? 谢谢。
答
假设你的文件包含每行一个网址,你可以这样做:
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
答
with open('list.txt') as my_list:
for url in my_list:
wget.download(url)
好吧,似乎它的工作原理。但是如果网站受到用户名和密码的保护呢? 这种情况下的相同脚本返回 urllib2.HTTP错误:HTTP错误401:需要授权 我该如何解决问题? – user2044983 2013-02-15 14:15:29
请参阅http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly上的示例 – mbatchkarov 2013-02-15 14:23:08