从网页中提取样式规则

时间:2019-07-04 08:26:10

标签: html css python-3.x web-scraping

我想从网页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)

0 个答案:

没有答案