解析HTML:Python中的lxml错误

2024-09-28 05:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个简单的脚本来从here获取大的灰色表。在

我的代码如下:

import urllib2
from lxml import etree

html = urllib2.urlopen("http://www.afi.com/100years/movies10.aspx").read()

root = etree.XML(html)

但是我得到了一个关于最后一个陈述的错误。在

^{pr2}$

你知道我怎样才能避免这个错误吗?在

谢谢。在


Tags: 代码fromimport脚本httpherehtmlwww
2条回答

您尝试用XML解析器解析HTML,应该使用lxml HTML解析器。在

import urllib2
from StringIO import StringIO
from lxml import etree

ufile = urllib2.urlopen("http://www.afi.com/100years/movies10.aspx")

root = etree.parse(ufile, etree.HTMLParser())

print etree.tostring(root)

链接到的文档不是格式正确的XHTML,因此不能使用XML解析器来加载它。在

您必须使用类似Beautiful Soup的HTML解析器。在

相关问题 更多 >

    热门问题