-1

我有以下代码:

import urllib2
from itertools import product

with open('urllist.txt') as urllist:
    urls=[line.strip() for line in urllist]

for url in product(urls):
    usock = urllib2.urlopen(url)
    data = usock.read()
    usock.close()
    sourcecode=open('./sourcecode', 'w+')
    sourcecode.write(data)

当我运行它时,它给出了:

Traceback (most recent call last):
  File "12.py", line 8, in <module>
    usock = urllib2.urlopen(url)
  File "/opt/python2.7.1/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/opt/python2.7.1/lib/python2.7/urllib2.py", line 383, in open
    req.timeout = timeout
AttributeError: 'tuple' object has no attribute 'timeout'

知道如何解决吗?非常感谢!

4

1 回答 1

3

itertools.product返回一个元组而不是项目本身。:

>>> from itertools import product
>>> lis = ['a','b','c']
>>> for p in product(lis):
...     print p
...     
('a',)
('b',)
('c',)

在 url 上使用一个简单的循环:

for url in urls:
    usock = urllib2.urlopen(url)
于 2013-07-07T17:42:38.857 回答