lxml etree 解析失败(IOError)

lxml etree parse failure (IOError)

我正在尝试 运行 这个程序。直到今天它都运行良好。我这边的代码没有任何改变。

import lxml.etree
import urlparse
import re

def parse_url(url):
    return lxml.etree.parse(url, lxml.etree.HTMLParser())

urlivv = "http://finance.yahoo.com/q?s=IVV"
docivv = parse_url(urlivv)

这是我的错误信息:

IOError: Error reading file 'http://finance.yahoo.com/q?s=IVV': failed to load external entity "http://finance.yahoo.com/q?s=IVV"

网站上有一些关于将 StringIO 添加到参数中的文档(见下文)。但我觉得很奇怪,我以前从来没有这样做过。

tree = etree.parse(StringIO(myString))

编辑:更完整的堆栈跟踪。

>>> import lxml.etree
>>> tree = lxml.etree.parse('http://finance.yahoo.com/q?s=IVV', parser=lxml.etree.HTMLParser())
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "src\lxml\lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:81100)
  File "src\lxml\parser.pxi", line 1811, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:117831)
  File "src\lxml\parser.pxi", line 1837, in lxml.etree._parseDocumentFromURL (src\lxml\lxml.etree.c:118178)
  File "src\lxml\parser.pxi", line 1741, in lxml.etree._parseDocFromFile (src\lxml\lxml.etree.c:117090)
  File "src\lxml\parser.pxi", line 1138, in lxml.etree._BaseParser._parseDocFromFile (src\lxml\lxml.etree.c:111636)
  File "src\lxml\parser.pxi", line 595, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:105092)
  File "src\lxml\parser.pxi", line 706, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:106800)
  File "src\lxml\parser.pxi", line 633, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:105611)
OSError: Error reading file 'http://finance.yahoo.com/q?s=IVV': failed to load external entity "http://finance.yahoo.com/q?s=IVV"

如果您正在寻找解释,请立即停止阅读。另一方面,如果您寻求补救措施,那么可能就是这样。

>>> page = requests.get('http://finance.yahoo.com/q?s=IVV').text
>>> lxml.etree.fromstring(page) #fails
>>> tree = lxml.etree.fromstring(page, parser=lxml.etree.HTMLParser())
>>> tree.xpath('.//table[@class="W(100%)"]')
[<Element table at 0x607dc08>]

简而言之,使用 requests 以字符串形式获取页面,使用 lxml 解析该字符串并照常继续。

编辑:在此代码 运行 之前,您需要 import requestsimport lxml.etree