【问题标题】:find if background image is used for any html tag with inline style查找背景图像是否用于任何具有内联样式的 html 标记
【发布时间】:2012-08-03 00:27:37
【问题描述】:

如何找到所有有背景图片的标签? 没有使用样式表。所有样式都内联在标签中。

我目前正在使用 lxml 和 xpath。这是我能走多远:

from lxml import html  
html_text = """somehtml"""

doc = html.fromstring(html_text)  
tagWithBackgrounds = doc.xpath("//*[contains(@style,'background')]")

接下来呢?

【问题讨论】:

    标签: python html parsing xpath lxml


    【解决方案1】:

    doc.xpath('//*[contains(@style,"background") and contains(@style,"url(")]')

    不完美,但有效。

    【讨论】:

      【解决方案2】:

      根据xmlme.com's XpathTool,这个 xPath 应该可以工作:

      //*[contains(@style,'background-image')]
      

      这是我的测试结果:

      【讨论】:

      • 但是可以使用background 属性来指定图像,不一定是background-image
      • 哦,我明白了。我认为您不能仅使用 Xpath 进行这种模式识别。您应该首先获取在其内联样式中包含单词background 的所有节点,然后遍历它们并使用正则表达式确认它们的样式包含背景图像。
      • 是的,无法得到完美的答案。 URL匹配本身就是一个很大的挑战。但我为我的目的做了一个足够好的 xpath 表达式。
      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 2013-01-10
      • 2017-10-21
      • 2011-06-17
      • 2021-05-27
      • 2021-07-01
      • 1970-01-01
      • 2019-03-19
      相关资源
      最近更新 更多