如何获取lxml元素的css属性? [英] How to get css attribute of a lxml element?

查看:312
本文介绍了如何获取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) 

这篇关于如何获取lxml元素的css属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆