Python 使用通配符在 XML 中查找标签

标签 python xml lxml wildcard

我的 python 脚本中有这一行:

url = tree.find("//video/products/product/read_only_info/read_only_value[@key='storeURL-GB']")

但有时 storeURL-GB 键会更改最后两个国家代码字母,所以我尝试使用类似这样的方法,但它不起作用:

url = tree.find("//video/products/product/read_only_info/read_only_value[@key='storeURL-\.*']")

有什么建议吗?

最佳答案

你或许应该试试 .xpath()starts-with():

urls = tree.xpath("//video/products/product/read_only_info/read_only_value[starts-with(@key, 'storeURL-')]")
if urls:
    url = urls[0]

关于Python 使用通配符在 XML 中查找标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19294939/

相关文章:

python - 使用 Python 3 提取 XML 元素和属性数据

python - BeautifulSoup,Python 3,编码错误

python - 获取域中所有页面的所有内部链接

python - 以不同格式导出数据集

python - 有没有办法安全地存储 SHA256 哈希的内部状态

python - 在两个 Python 脚本中将文件作为参数传递

python - 如何使用groupby获取与列的最大值对应的所有行

arrays - 迭代 CSV 并创建 XML 文件

Android表格列宽50/50

python - LXML + python : How to change every instance of an element