小问题,真的卡在这里,我不明白发生了什么,我只想从网络上解析一个普通的xhtml,没什么特别的......
这是错误:
File "class/page.py", line 85, in xslParse
doc = lxml.etree.fromstring(self.content)
File "lxml.etree.pyx", line 2753, in lxml.etree.fromstring (src/lxml/lxml.etree.c:54647)
File "parser.pxi", line 1578, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:82764)
File "parser.pxi", line 1457, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:81562)
File "parser.pxi", line 965, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:78232)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74488)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75379)
File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74712)
XMLSyntaxError: StartTag: invalid element name, line 1, column 2
self.content 是http 响应给定的普通字符串,没有清理,没有替换,什么都没有,只是服务器的响应,那是什么鬼..?
html的开头是:
<!doctype html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7 ]> <html lang="fr" class="no-js ie6" itemscope itemtype="http://schema.org/Product"> <![endif]-->
<!--[if IE 7 ]> <html lang="fr" class="no-js ie7" itemscope itemtype="http://schema.org/Product"> <![endif]-->
<!--[if IE 8 ]> <html lang="fr" class="no-js ie8" itemscope itemtype="http://schema.org/Product"> <![endif]-->
<!--[if IE 9 ]> <html lang="fr" class="no-js ie9" itemscope itemtype="http://schema.org/Product"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js" itemscope itemtype="http://schema.org/Product"> <!--<![endif]-->
<head>......
一个普通的网页,为什么lxml不能解析一个普通的格式良好的文档?
最佳答案
<!doctype html>
表示它是一个使用 HTML 语法的 HTML5 文档。所以你应该使用 HTML(不是 XML)解析器。为了进行比较,XML 文档可能以 <?xml version="1.0" encoding="UTF-8"?>
开头.
你可以使用 lxml.html.fromstring()
作为@unutbu suggested in the comments .
如果您通过 HTTP 接收页面,那么使用 XML 语法的 HTML5 文档应该具有 XML 媒体类型,例如 application/xhtml+xml
或 application/xml
而不是例如 text/html
用于 HTML 语法。
关于python - 使用 lxml python 解析 xhtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917270/