0

我试图每 5 秒从一个站点提取信息,但它似乎不起作用,每次运行它时都会出错。

下面的代码:

import urllib2, threading
def readpage():
    data = urllib2.urlopen('http://forums.zybez.net/runescape-2007-prices').read()
    for line in data:
        if 'forums.zybez.net/runescape-2007-prices/player/' in line:
            a = line.split('/runescape-2007-prices/player/'[1])
            print(a.split('">')[0])

t = threading.Timer(5.0, readpage)
t.start()

我收到这些错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 808, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 1080, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\Jordan\Desktop\username.py", line 3, in readpage
    data = urllib2.urlopen('http://forums.zybez.net/runescape-2007-prices').rea
()
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden

帮助将不胜感激,谢谢!

4

3 回答 3

0

您是否尝试在没有线程的情况下打开该网址?错误代码显示 403: Forbidden,也许您需要对该网页进行身份验证。

于 2013-10-24T21:04:09.510 回答
0

这与 Python 无关——服务器拒绝您对该 URL 的请求。

我怀疑 URL 不正确,或者您遇到了某种速率限制并被阻止。

编辑:如何使它工作

该网站正在阻止 Python 的User-Agent. 试试这个:

import urllib2, threading
def readpage():

    headers = { 'User-Agent' : 'Mozilla/5.0' }
    req = urllib2.Request('http://forums.zybez.net/runescape-2007-prices', None, headers)
    data = urllib2.urlopen(req).read()

    for line in data:
        if 'forums.zybez.net/runescape-2007-prices/player/' in line:
            a = line.split('/runescape-2007-prices/player/'[1])
            print(a.split('">')[0])
于 2013-10-24T21:04:13.700 回答
0

该站点拒绝 urllib2 报告的默认用户代理。您可以使用 install_opener 为脚本中的所有请求更改它。

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0')]
urllib2.install_opener(opener)

您还需要按站点拆分数据以逐行读取

urllib2.urlopen('http://forums.zybez.net/runescape-2007-prices').read().splitlines()

和改变

line.split('/runescape-2007-prices/player/'[1])

line.split('/runescape-2007-prices/player/')[1]

在职的:

import urllib2, threading

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0')]
urllib2.install_opener(opener)

def readpage():
    data = urllib2.urlopen('http://forums.zybez.net/runescape-2007-prices').read().splitlines()
    for line in data:
        if 'forums.zybez.net/runescape-2007-prices/player/' in line:
            a = line.split('/runescape-2007-prices/player/')[1]
            print(a.split('">')[0])

t = threading.Timer(5.0, readpage)
t.start()
于 2013-10-24T21:18:57.883 回答