【问题标题】:Python find tag in XML using wildcardPython使用通配符在XML中查找标签
【发布时间】:2013-10-10 11:46:52
【问题描述】:

我的 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-\.*']")

有什么建议吗?

【问题讨论】:

    标签: python xml lxml wildcard


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2021-11-17
      相关资源
      最近更新 更多