我有一个python脚本来获取网页并对其进行镜像。它适用于一个特定的页面,但我不能让它工作多个。我以为我可以把多个网址变成一个列表,然后哺养的功能,但我得到这个错误:使用urlopen打开网址列表
Traceback (most recent call last):
File "autowget.py", line 46, in <module>
getUrl()
File "autowget.py", line 43, in getUrl
response = urllib.request.urlopen(url)
File "/usr/lib/python3.2/urllib/request.py", line 139, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.2/urllib/request.py", line 361, in open
req.timeout = timeout
AttributeError: 'tuple' object has no attribute 'timeout'
这里是有问题的代码:
url = ['https://www.example.org/', 'https://www.foo.com/', 'http://bar.com']
def getUrl(*url):
response = urllib.request.urlopen(url)
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
getUrl()
我已经用尽谷歌试图找到如何用urlopen()打开一个列表。我找到了一种这样的作品。它需要一个.txt
文档并逐行阅读,将每一行作为URL提供,但我正在使用Python 3编写此文档,并且由于某种原因twillcommandloop
将不会导入。另外,这种方法很笨拙,需要(据说)不必要的工作。
无论如何,任何帮助将不胜感激。
你为什么不简单地用'for'循环迭代你的URL列表? –
回复sheng的评论时才想起来!它会将特定部分作为字符串返回,对吗? –