IOError:读取文件时出错:加载HTTP资源失败,Pythonywhere中出现LXML错误

IOError:读取文件时出错:加载HTTP资源失败,Pythonywhere中出现LXML错误,python,python-2.7,lxml,pythonanywhere,Python,Python 2.7,Lxml,Pythonanywhere,我在Python2.7中使用lxml时遇到问题。我试着安装lxml版本3.4.0和3.4.2,但得到了相同的错误,不知道为什么。以下是我的python代码: @app.route("/getInformation", methods=['GET']) def domain(): urlList = [] urlList.append("http://gbgfotboll.se/serier/?scr=table&ftid=57109")

我在Python2.7中使用lxml时遇到问题。我试着安装lxml版本3.4.0和3.4.2,但得到了相同的错误,不知道为什么。以下是我的python代码:

@app.route("/getInformation", methods=['GET'])           
def domain(): 

    urlList = []

    urlList.append("http://gbgfotboll.se/serier/?scr=table&ftid=57109")
    urlList.append("http://gbgfotboll.se/serier/?scr=table&ftid=57108")

    date = '2015-04-18'

    # use this in real mode: currentDate = (time.strftime("%Y-%m-%d"))

    homeScore = "0"
    awayScore = "0"
    homeTeam = ""
    awayTeam = ""

    time_xpath = XPath("td[1]/span/span//text()[2]")
    team_xpath = XPath("td[2]/a/text()")
    league_xpath = XPath("//*[@id='content-primary']/h1//text()")

    for url in urlList:
        test =  0 #remove this
        rows_xpath = XPath("//*[@id='content-primary']/table/tbody/tr[td[1]/span/span//text()='%s']" % (date))
        html = lxml.html.parse(url)
    ....
下面是我得到的错误:

2015-03-28 13:12:23,852 :Exception on /getInformation [GET]
Traceback (most recent call last):
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Timocin/mysite/work.py", line 48, in domain
    html = lxml.html.parse(url)
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/lxml/html/__init__.py", line 786, in parse
    return etree.parse(filename_or_url, parser, base_url=base_url, **kw)
  File "lxml.etree.pyx", line 3299, in lxml.etree.parse (src/lxml/lxml.etree.c:72655)
  File "parser.pxi", line 1791, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:106263)
  File "parser.pxi", line 1817, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:106564)
  File "parser.pxi", line 1721, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:105561)
  File "parser.pxi", line 1122, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:100456)
  File "parser.pxi", line 580, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:94543)
  File "parser.pxi", line 690, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:96003)
  File "parser.pxi", line 618, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:95015)
IOError: Error reading file 'http://gbgfotboll.se/serier/?scr=table&ftid=57109': failed to load HTTP resource
我在另一个上使用了相同的代码,效果很好。但现在我换了服务器,我不知道为什么会出现这个错误。有什么想法吗

编辑1

我尝试改用urllib2,得到了以下消息:

2015-03-28 15:15:05,087 :Exception on /getInformation [GET]
Traceback (most recent call last):
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/Timocin/mysite/env/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Timocin/mysite/work.py", line 57, in domain
    p = urlopen(url)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
编辑2

我最近发现,你需要有一个付费帐户才能访问外部网站。我购买了一个帐户,但它仍然不工作,但是错误消息发生了变化,对于lxml:

IOError: Error reading file 'https://gbgfotboll.se/serier/?scr=table&ftid=57109': failed to load external entity "https://gbgfotboll.se/serier/?scr=table&ftid=57109"
对于urllib2

URLError: <urlopen error [Errno 111] Connection refused>

这可能是网络问题吗?IOError:读取文件时出错:无法加载HTTP资源->网络连接?正如我说的,我在www.pythonanywhere.com上运行它,所以我怀疑他们的网络没有连接@KlausD.PythonAnywhere dev在这里-我们也在论坛中讨论这个问题,因此我将添加此链接,以便其他有相同问题的人可以关注这里的讨论: