我的 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/