2

好的,我正在尝试使用带有漂亮汤的开瓶器从页面中提取一些信息,我认为这就是问题所在。我需要使用 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'
4

1 回答 1

6

看起来您正在使用 opener 对象,就好像它是一个 URL:

page = BeautifulSoup(urllib2.urlopen(url).read())

url打开的开瓶器在哪里...相反,请执行以下操作:

page = BeautifulSoup(url.read())
于 2013-02-15T00:57:19.880 回答