【发布时间】:2011-10-15 03:47:54
【问题描述】:
我在使用带有 XHTML(或带有命名空间的 XML)的 cssselect 时遇到问题。虽然文档说如何在 csselect 中使用命名空间,但我不明白:cssselect namespaces
我的输入 XHTML 字符串:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Teststylesheet</title>
<style type="text/css">
/*<![CDATA[*/
ol{margin:0;padding:0}
/*]]>*/
</style>
</head>
<body>
</body>
</html>
我的 Python 脚本:
parser = etree.XMLParser()
tree = etree.fromstring(xhtmlstring, parser).getroottree()
for style in CSSSelector("style")(tree):
print "HAVE CSS!"
python 脚本不打印任何Have CSS!。使用etree.HTMLParser 而不是etree.XMLParser 可以,但我真的很想使用XMLParser 并保留XHTML 的所有内容(命名空间、结构)。
谁能帮我解决这个命名空间问题?
【问题讨论】:
标签: python css xhtml lxml xml-namespaces