Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python find tag in XML using wildcard

I have this line in my python script:

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:

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

Any suggestions please?

like image 965
speedyrazor Avatar asked Mar 22 '23 02:03

speedyrazor


1 Answers

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

urls = tree.xpath("//video/products/product/read_only_info/read_only_value[starts-with(@key, 'storeURL-')]")
if urls:
    url = urls[0]
like image 66
paul trmbrth Avatar answered Apr 01 '23 12:04

paul trmbrth