python timer + urllib2代码错误

时间:2013-10-24 21:00:46

标签: python

我试图从网站上提取信息的时间是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

帮助将不胜感激,谢谢!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这与Python无关 - 服务器拒绝您对该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])

答案 2 :(得分: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()