python - lxml 中的解析函数出错

标签 python windows parsing lxml

我已经在 windows 平台上安装了 lxml2.2.2(我使用的是 python 版本 2.6.5)。我尝试了这个简单的命令:

from lxml.html import parse 
p= parse(‘http://www.google.com’).getroot()

但我收到以下错误:

Traceback (most recent call last):
File “”, line 1, in p=parse(‘http://www.google.com’).getroot()
File “C:\Python26\lib\site-packages\lxml-2.2.2-py2.6-win32.egg\lxml\html_init_.py”, line 661, in parse return etree.parse(filenameorurl, parser, baseurl=baseurl, **kw) 
File “lxml.etree.pyx”, line 2698, in lxml.etree.parse (src/lxml/lxml.etree.c:49590) 
File “parser.pxi”, line 1491, in lxml.etree.parseDocument (src/lxml/lxml.etree.c:71205) File “parser.pxi”, line 1520, in lxml.etree.parseDocumentFromURL (src/lxml/lxml.etree.c:71488) 
File “parser.pxi”, line 1420, in lxml.etree.parseDocFromFile (src/lxml/lxml.etree.c:70583)
File “parser.pxi”, line 975, in lxml.etree.BaseParser.parseDocFrom
File (src/lxml/lxml.etree.c:67736)
File “parser.pxi”, line 539, in lxml.etree.ParserContext.handleParseResultDoc (src/lxml/lxml.etree.c:63820) 
File “parser.pxi”, line 625, in lxml.etree.handleParseResult (src/lxml/lxml.etree.c:64741)
File “parser.pxi”, line 563, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64056)
IOError: Error reading file ‘http://www.google.com’: failed to load external entity “http://www.google.com”

作为 python 的新手,我不知道下一步该怎么做。请指导我解决这个错误。提前致谢!! :)

最佳答案

lxml.html.parse 不获取 URL。

以下是使用 urllib2 的方法:

>>> from urllib2 import urlopen
>>> from lxml.html import parse
>>> page = urlopen('http://www.google.com')
>>> p = parse(page)
>>> p.getroot()
<Element html at 1304050>

更新
史蒂文是对的。 lxml.etree.parse 应该接受并加载 URL。我错过了。我试过删除这个答案,但不允许。

我收回关于它不获取 URL 的声明。

关于python - lxml 中的解析函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3116269/

相关文章:

python - 从具有重复键 Python 的字符串创建一个 dict/json

python - 我可以使用从异常 block 调用更高的异常 block 吗?

python - "Error 1053: The service did not respond timely",无法从 Python 代码启动使用 cx_Freeze 创建的 Windows 服务

php - Laravel ⁠ 在字符串上存储 2 个值的集合以供解析并稍后显示它们

python - Tkinter 检查输入框是否为空

python - 如果我分别使用 pip 和 pip3 安装 virtualenv,默认值是多少?

c - CGO对“TIFFGetField”的 undefined reference

windows - 是否有 OutputDebugString 的 Unicode 替代品?

c++ - 解析具有已知变量的字符串中的逻辑表达式

python - 美丽汤 4 + python : string returns 'None'