【发布时间】:2015-02-13 08:17:02
【问题描述】:
有这样的 xml 文件。我怎样才能只选择那个标签,href 属性以parent 结尾,就像下面的第三个元素。
按位置确定,例如
elem = tree.findall('{*}CustomProperty')[2]
不适合,因为某些文档可能只有一个 parent href,其他 5-10 和第三个可能根本没有这样的 href。
我倾向于使用 xpath,但不知道如何告诉 xpath 搜索属性匹配的结尾。
xpath 也不是必须的,我很乐意使用任何适合我目的的方式
那么我怎样才能获得CustomProperty 元素,它的href 属性以单词parent 结尾?
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566:parent">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
提前感谢您的帮助
【问题讨论】:
标签: python xml xpath web-scraping lxml