etree.findall:'OR'-查找?
我想使用 lxml.etree.findall
查找 XHTML 文件中的所有样式表定义。这可能很简单,
elems = tree.findall('link[@rel="stylesheet"]') + tree.findall('style')
但是 CSS 样式定义的问题在于顺序很重要,例如,
<link rel="stylesheet" type="text/css" href="/media/css/first.css" />
<style>body:{font-size: 10px;}</style>
<link rel="stylesheet" type="text/css" href="/media/css/second.css" />
如果 style
标记的内容在两个 link
中的规则之后应用标签,结果可能与按定义顺序应用规则的结果完全不同。
那么,我将如何进行包含 link[@rel="stylesheet"]
和 style
的查找?
I want to find all stylesheet definitions in a XHTML file with lxml.etree.findall
. This could be as simple as
elems = tree.findall('link[@rel="stylesheet"]') + tree.findall('style')
But the problem with CSS style definitions is that the order matters, e.g.
<link rel="stylesheet" type="text/css" href="/media/css/first.css" />
<style>body:{font-size: 10px;}</style>
<link rel="stylesheet" type="text/css" href="/media/css/second.css" />
if the contents of the style
tag is applied after the rules in the two link
tags, the result may be completely different from the one where the rules are applied in order of definition.
So, how would I do a lookup that inlcudes both link[@rel="stylesheet"]
and style
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用 XPATH:
Possible using XPATH: