我正在尝试运行此程序。它一直工作到今天。我的代码中没有更改任何内容。
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"
答案 0 :(得分:3)
如果您正在寻找解释,可以立即停止阅读。另一方面,如果你寻求补救,那么可能就是这样。
>>> 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 requests
和import lxml.etree
。