xpath解析XML简单明了,但是XML有命名空间的话就会出错了。解决方法是节点前加命名空间的前缀,下例中x、y是变量可以任意定义。
例如XML文档如下:
Text
tree = etree.parse(path) root = tree.getroot() for child in root: r=child.xpath('x:a/y:b/text()',namespaces={'x': 'www.jingfengshuo.com', 'y': 'jingfengshuo.com'})[0]
Text
tree = etree.parse(path) root = tree.getroot() for child in root: r=child.xpath('x:a/x:b/text()',namespaces={'x': 'www.jingfengshuo.com'})[0]
没有评论:
发表评论