AttributeError: addinfourl实例没有'response_type'属性
好的,我正在尝试用Beautiful Soup和opener来从一个页面提取一些信息,我觉得问题就出在这里。我需要使用opener,因为我需要通过Tor来访问这个页面,因为我觉得他们已经屏蔽了多次请求。
(如果这些内容没有格式化好,我会马上进行编辑,因为通常会发生一些奇怪的事情。)
这是代码:
def getsite():
proxy = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
url = opener.open('https://www.website.com')
try:
page = BeautifulSoup(urllib2.urlopen(url).read())
except Exception as Err:
errorlist.append('Unexpected Error ' + str(Err))
time.sleep(60)
page = BeautifulSoup(urllib2.urlopen(url).read())
values = page.findAll("strong")
high = values[2]
low = values[1]
last = values[0]
vol = values[3]
high = str(high)
low = str(low)
last = str(last)
vol = str(vol)
high = high[8:-13]
low = low[8:-13]
last = last[8:-13]
vol = vol[8:-24]
print high, low, last, vol
while True:
getsite()
time.sleep(3200)
然后它出现了这个错误。
page = BeautifulSoup(urllib2.urlopen(url).read()) File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 392, in open
protocol = req.get_type() AttributeError: addinfourl instance has no attribute 'get_type'
1 个回答
7
看起来你把 opener 对象当成了一个网址来使用:
page = BeautifulSoup(urllib2.urlopen(url).read())
这里的 url
是你打开的 opener... 其实应该这样做:
page = BeautifulSoup(url.read())