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

时间:2023-01-14 08:02:48

I have this line in my python script:

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

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

but sometimes the storeURL-GB key changes the last two country code letters, so I am trying to use something like this, but it doesn't work:

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

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

Any suggestions please?

有什么建议吗?

1 个解决方案

#1


6  

You should probably try .xpath() and starts-with():

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

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

#1


6  

You should probably try .xpath() and starts-with():

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

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