3

我正在使用 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>
4

1 回答 1

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)
于 2012-11-24T15:18:03.073 回答