我想从网页html中提取样式规则。我看过一些cssutils之类的python库。但是我无法提取样式标签
# -*- coding: utf-8 -*-
import cssutils
with open('/home/root1/Downloads/FontSrc.css') as file:
css = file.read()
# css = u'''/* a comment with umlaut ä */
# @namespace html "http://www.w3.org/1999/xhtml";
# @variables { BG: #fff }
# html|a { color:red; background: var(BG) }'''
sheet = cssutils.parseString(css)
for rule in sheet:
if rule.type == rule.STYLE_RULE:
# find property
for property in rule.style:
if property.name == 'color':
property.value = 'green'
property.priority = 'IMPORTANT'
break
# or simply:
rule.style['margin'] = '01.0eM' # or: ('1em', 'important')
print(sheet.cssText)