如何获取lxml元素的css属性?
问题描述:
我想找到一个快速的函数来获取lxml元素的所有样式属性,这些属性应考虑css样式表,style属性元素并解决继承问题.
I want to find a fast function to get all style properties of a lxml element that take into account the css stylesheet, the style attribute element and tackle the herit issue.
例如:
html:
<body>
<p>A</p>
<p id='b'>B</p>
<p style='color:blue'>B</p>
</body>
css:
body {color:red;font-size:12px}
p.b {color:pink;}
python:
elements = document.xpath('//p')
print get_style(element[0])
>{color:red,font-size:12px}
print get_style(element[1])
>{color:pink,font-size:12px}
print get_style(element[2])
>{color:blue,font-size:12px}
谢谢
答
您可以结合使用lxml和 cssutils . 此 cssutils实用程序模块应该可以你在问什么与该模块一起安装cssutils,然后运行以下代码:
You can do this with a combination of lxml and cssutils. This cssutils utility module should be able to do what you're asking. Install cssutils along with that module, then run the following code:
from style import *
html = """<body>
<p>A</p>
<p id='b'>B</p>
<p style='color:blue'>B</p>
</body>"""
css = """body {color:red;font-size:12px}
p {color:yellow;}
p.b {color:green;}"""
def get_style(element, view):
if element != None:
inline_style = [x[1] for x in element.items() if x[0] == 'style']
outside_style = []
if view.has_key(element):
outside_style = view[element].getCssText()
r = [[inline_style, outside_style]]
r.append(get_style(element.getparent(), view))
return r
else:
return None
document = getDocument(html)
view = getView(document, css)
elements = document.xpath('//p')
print get_style(elements[0], view)