【发布时间】:2016-03-10 11:37:23
【问题描述】:
我正在尝试使用the solutions in this post 将 HTML 表解析为 python (2.7)。 当我用字符串尝试前两个中的任何一个时(如示例中所示),它工作得很好。 但是,当我尝试在使用 urlib 阅读的 HTML 页面上使用 etree.xml 时,出现错误。我检查了每个解决方案,我传递的变量也是一个 str 。 对于以下代码:
from lxml import etree
import urllib
yearurl="http://www.boxofficemojo.com/yearly/chart/?yr=2014&p=.htm"
s=urllib.urlopen(yearurl).read()
print type (s)
table = etree.XML(s)
我收到此错误:
文件“C:/Users/user/PycharmProjects/Wikipedia/TestingFile.py”,行 9、in table = etree.XML(s)
文件“lxml.etree.pyx”,第 2723 行,在 lxml.etree.XML (src/lxml/lxml.etree.c:52448)
文件“parser.pxi”,第 1573 行,在 lxml.etree._parseMemoryDocument 中 (src/lxml/lxml.etree.c:79932)
文件“parser.pxi”,第 1452 行,在 lxml.etree._parseDoc (src/lxml/lxml.etree.c:78774)
文件“parser.pxi”,第 960 行,在 lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:75389)
文件“parser.pxi”,第 564 行,在 lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
文件“parser.pxi”,第 645 行,在 lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
文件“parser.pxi”,第 585 行,在 lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71955) lxml.etree.XMLSyntaxError: 打开和 结束标签不匹配:链接第 8 行和头部,第 8 行,第 48 列
对于这段代码:
from xml.etree import ElementTree as ET
import urllib
yearurl="http://www.boxofficemojo.com/yearly/chart/?yr=2014&p=.htm"
s=urllib.urlopen(yearurl).read()
print type (s)
table = ET.XML(s)
我收到此错误:
Traceback(最近一次调用最后一次):文件 “C:/Users/user/PycharmProjects/Wikipedia/TestingFile.py”,第 6 行,在 表 = ET.XML(s)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第 1300 行,以 XML 格式 parser.feed(文本)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第 1642 行,在提要中 self._raiseerror(v)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第 1506 行,在 _raiseerror raise err xml.etree.ElementTree.ParseError: mismatched tag: line 8, column 111
【问题讨论】:
标签: python python-2.7 html-parsing elementtree