使用python提取CSS样式声明

时间:2012-10-23 10:51:21

标签: python html css lxml

我正在使用lxml.html解析一个html文件,但是,我需要获取在样式表中用作选择器的HTML类,即['c1','c2','c3','c4','c5','c6']以及相应的样式信息。

我将样式部分提取为字符串尝试使用cssutils.parseString解析它但我最终得到了这个:

ERROR   CSSStyleRule: No start { of style declaration found: u'<style type="text/css">&#13;' [1:28: ;]
ERROR   Selector: Unexpected CHAR. [1:1: <]
ERROR   Selector: Unexpected CHAR. [1:12: =]
ERROR   Selector: Unexpected STRING. [1:13: "text/css"]
ERROR   Selector: Unexpected CHAR. [1:24: &]
ERROR   SelectorList: Invalid Selector: <style type="text/css">&#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'<style type="text/css">&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [2:47: ;]
ERROR   Selector: Unexpected CHAR. [2:43: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [3:33: ;]
ERROR   Selector: Unexpected CHAR. [3:29: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [4:32: ;]
ERROR   Selector: Unexpected CHAR. [4:28: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [5:34: ;]
ERROR   Selector: Unexpected CHAR. [5:30: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [6:34: ;]
ERROR   Selector: Unexpected CHAR. [6:30: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'&#13;' [7:53: ;]
ERROR   Selector: Unexpected CHAR. [7:49: &]
ERROR   SelectorList: Invalid Selector: &#13
ERROR   CSSStyleRule: No style declaration or "}" found: u'&#13;'
ERROR   CSSStyleRule: No start { of style declaration found: u'</style>' [8:13: ]
ERROR   Selector: Unexpected CHAR. [8:5: <]
ERROR   Selector: Unexpected CHAR. [8:6: /]
ERROR   Selector: Cannot end with combinator: </style>
ERROR   SelectorList: Invalid Selector: </style>
ERROR   CSSStyleRule: No style declaration or "}" found: u'</style>'
<cssutils.css.CSSStyleSheet object encoding='utf-8' href=None media=None title=None namespaces={} at 0x308ca90>

我该如何解决这个问题?

<style type="text/css">&#13;
    p.c6 {font-weight: bold; text-align: left}&#13;
    p.c5 {font-weight: bold}&#13;
    p.c4 {text-align: left}&#13;
    td.c3 {font-weight: bold}&#13;
    p.c2 {text-align: center}&#13;
    p.c1 {font-weight: bold; text-align: center}&#13;
</style>

1 个答案:

答案 0 :(得分:0)

您使用什么代码来获取该错误消息?

CSS解析器(如cssutils或tinycss)希望您提供CSS源代码。您似乎正在提供HTML <style>元素的来源。您可以使用lxml.html来解析该HTML。然后,您应该提取您感兴趣的节点的text属性,而不是将它们序列化为HTML。

您的代码应该是这样的:

html = '<style>a.b &#123;}</style>'
lxml.html.fromstring(html)
for style_element in html.xpath('style')
    # lmxl parses HTML entities like &#123;
    assert style_element.text == 'a.b {}'
    stylesheet = cssutils.parseString(style_element.text)