我正在使用 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"> ' [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">
ERROR CSSStyleRule: No style declaration or "}" found: u'<style type="text/css"> '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [2:47: ;]
ERROR Selector: Unexpected CHAR. [2:43: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [3:33: ;]
ERROR Selector: Unexpected CHAR. [3:29: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [4:32: ;]
ERROR Selector: Unexpected CHAR. [4:28: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [5:34: ;]
ERROR Selector: Unexpected CHAR. [5:30: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [6:34: ;]
ERROR Selector: Unexpected CHAR. [6:30: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
ERROR CSSStyleRule: No start { of style declaration found: u' ' [7:53: ;]
ERROR Selector: Unexpected CHAR. [7:49: &]
ERROR SelectorList: Invalid Selector: 
ERROR CSSStyleRule: No style declaration or "}" found: u' '
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">
p.c6 {font-weight: bold; text-align: left}
p.c5 {font-weight: bold}
p.c4 {text-align: left}
td.c3 {font-weight: bold}
p.c2 {text-align: center}
p.c1 {font-weight: bold; text-align: center}
</style>