python绑定到html5ever
htmlpyever的Python项目详细描述
htmlpyever是一个非常专一的html5绑定。您可以:
输入解析器:
parser.feed(b'hOI wURLD!')
当解析器遇到结束脚本标记时获取回调:
defscript_callback(script):# handle scriptparser=htmlpyever.Parser(script_callback)# orclassMyParser(htmlpyever.Parser):defrun_script(self,script)# handle scriptparser=MyParser()
以lxmlElement或ElementTree形式获取结果:
fromlxmlimportetreeetree.tostring(parser.root)# >>> '<html><head/><body>hOI! wURLD!</body></html>'etree.tostring(parser.root)# >>> '<html><head/><body>hOI! wURLD!</body></html>'# not sure why the doctype doesn't show up in the serialized ElementTree
就这样。