CSS 选择器单独按属性查询,使用 LXML

CSS selectors to query by attribute alone, with LXML

我想获取属性包含 {% 的标签,例如这些示例:

<a href="{% route xy %}></a>
<img src="{% static xy %}/>

属性键无关紧要。

我能想到的最好的是 tag.cssselect[href*={%],但是当我想匹配所有属性而不考虑键时,它只匹配 hrefs。

如果可以的话,您可以使用 XPath 选择器来实现。 css selector is translated to XPath anyway-,像这样:

//*[@*[contains(.,'{%')]]

上面的 XPath 选择具有包含 "{%" 的任何属性的所有元素。以下是演示的工作示例:

from lxml.html import etree

html = """<div>
<a href="{% route xy %}"></a>
<img src="{% static xy %}"/>
</div>"""
root = etree.fromstring(html)
result = root.xpath("//*[@*[contains(.,'{%')]]")
for r in result:
    print etree.tostring(r)

输出:

<a href="{% route xy %}"/>

<img src="{% static xy %}"/>