我是lxml的新手。我想下载网页并从中获取感兴趣的数据,我的代码是:
import urllib2
from lxml import etree
url = "http://www.example.com/"
html = urllib2.urlopen(url)
root = etree.parse(html) # the problem is here
任何人都可以解释为什么这是错的吗?
错误是:
Traceback (most recent call last):
File "yatego.py", line 10, in <module>
root = etree.parse(html)
File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:54187)
File "parser.pxi", line 1550, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:79703)
File "parser.pxi", line 1580, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:80012)
File "parser.pxi", line 1463, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:78908)
File "parser.pxi", line 1019, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:75905)
File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
File "parser.pxi", line 585, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71955)
lxml.etree.XMLSyntaxError: Entity 'mdash' not defined, line 4, column 21
此代码:
url = "http://www.example.com/"
res = requests.get(url)
doc = lxml.html.parse(res.content)
给出了这个错误:
File "yatego.py", line 11, in <module>
doc = lxml.html.parse(res.content)
File "/usr/lib/python2.7/dist-packages/lxml/html/__init__.py", line 692, in parse
return etree.parse(filename_or_url, parser, base_url=base_url, **kw)
File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:54187)
File "parser.pxi", line 1528, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:79485)
File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:79768)
File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:78843)
File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:75698)
File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
File "parser.pxi", line 583, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71927)
IOError: Error reading file '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IANA — Example domains</title>
此代码:
doc = lxml.html.parse(url)
工作正常
问题出在哪里?
答案 0 :(得分:10)
这里的关键是例外:
IOError: Error reading file '<!DOCTYPE html PUBLIC ...
您将文件的内容传递给需要文件路径的函数。
同样的原因doc = lxml.html.parse(url)
有效,网址是&#34;文件路径。
以下工作是否更好?
doc = lxml.html.fromstring(res.content)
答案 1 :(得分:5)
您应该使用lxml.html
来解析HTML而不是lxml.etree
。
您也可以使用lxml
直接打开网址:
doc = lxml.html.parse(url)
有时lxml
在处理HTTP的怪癖时遇到问题,在这种情况下,您需要使用更强大的解决方案来获取页面,例如requests
:
res = requests.get(url)
doc = lxml.html.parse(res.content)
答案 2 :(得分:0)
您应该使用html.read()
开头:HTML不是字符串类型。此外,您应该检查URL是否正确下载,因为这无法保证。
UPD。使用html.parse(filename_or_url)