hga*_*tes 5 python lxml parsexml
小问题,真的卡在这里,我不明白发生了什么,我只是想从网上解析一个普通的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
Run Code Online (Sandbox Code Playgroud)
self.content是由http响应给出的普通字符串,没有干净,没有替换,没有,只是服务器的响应,所以什么是fu ..?
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>......
Run Code Online (Sandbox Code Playgroud)
一个普通的网页,为什么lxml无法解析普通的格式良好的文档?
jfs*_*jfs 11
<!doctype html>表示它是使用HTML语法的HTML5文档.所以你应该使用HTML(而不是XML)解析器.为了比较,XML文档可能从一开始就是<?xml version="1.0" encoding="UTF-8"?>.
你可以使用lxml.html.fromstring()作为@unutbu的意见建议.
如果您通过HTTP接收页面,那么使用XML语法的HTML5文档应该具有XML媒体类型,例如application/xhtml+xml或者application/xml代替text/htmlHTML语法.