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

spe*_*zor 4 python xml lxml wildcard

我在我的python脚本中有这一行:

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

但有时storeURL-GB键会更改最后两个国家/地区的代码字母,因此我尝试使用类似的东西,但它不起作用:

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

有什么建议吗?

pau*_*rth 7

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

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