python - 从任意 css 文件中获取特定类的所有 css 属性

标签 python css regex parsing

是否有任何相对简单的方法可以使用 python 中的某种解析器来获取一个类的组合属性,或者我是否必须想出一些正则表达式来获取它?

.container_12, .container_16 {
    margin-left:auto;
    margin-right:auto;
    width:960px
}
.grid_1, .grid_2, .grid_3, .grid_4, .grid_5 {
    display:inline;
    float:left;
    margin-left:10px;
    margin-right:10px
}
.featured_container .container_12 .grid_4 a {
    color: #1d1d1d;
    float: right;
    width: 235px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    border: 4px solid #141a20;

对于上面的 css 片段,如果我搜索“container_12”,它应该返回:

  {
        margin-left:auto;
        margin-right:auto;
        width:960px
        color: #1d1d1d;
        float: right;
        width: 235px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        border: 4px solid #141a20;
    }

重复的属性没问题,之后我会用字典来存储它们,所以不会有问题。

最佳答案

这是一个 CSS 的粗略解析器:

import pyparsing as pp

# punctuation is important during parsing, but just noise afterwords; suppress it
LBRACE, RBRACE = map(pp.Suppress, "{}")

# read a ':' and any following whitespace
COLON = (":" + pp.Empty()).suppress()

obj_ref = pp.Word(".", pp.alphanums+'_') | pp.Word(pp.alphas, pp.alphanums+'_')
attr_name = pp.Word(pp.alphas, pp.alphanums+'-_')
attr_spec = pp.Group(attr_name("name") + COLON + pp.restOfLine("value"))

# each of your format specifications is one or more comma-delimited lists of obj_refs,
# followed by zero or more attr_specs in {}'s
# using a pp.Dict will auto-define an associative array from the parsed keys and values
spec = pp.Group(pp.delimitedList(obj_ref)[1,...]('refs')
                + LBRACE
                + pp.Dict(attr_spec[...])("attrs")
                + RBRACE)

# the parser will parse 0 or more specs    
parser = spec[...]

解析你的CSS源:

result = parser.parseString(css_source)
print(result.dump())

给予:

[['.container_12', '.container_16', [['margin-left', 'auto;'], ['margin-right', 'auto;'], ['width', '960px']]], ['.grid_1', '.grid_2', '.grid_3', '.grid_4', '.grid_5', [['display', 'inline;'], ['float', 'left;'], ['margin-left', '10px;'], ['margin-right', '10px']]], ['.featured_container', '.container_12', '.grid_4', 'a', [['color', '#1d1d1d;'], ['float', 'right;'], ['width', '235px;'], ['height', '40px;'], ['text-align', 'center;'], ['line-height', '40px;'], ['border', '4px solid #141a20;']]]]
[0]:
  ['.container_12', '.container_16', [['margin-left', 'auto;'], ['margin-right', 'auto;'], ['width', '960px']]]
  - attrs: [['margin-left', 'auto;'], ['margin-right', 'auto;'], ['width', '960px']]
    - margin-left: 'auto;'
    - margin-right: 'auto;'
    - width: '960px'
  - refs: ['.container_12', '.container_16']
[1]:
  ['.grid_1', '.grid_2', '.grid_3', '.grid_4', '.grid_5', [['display', 'inline;'], ['float', 'left;'], ['margin-left', '10px;'], ['margin-right', '10px']]]
  - attrs: [['display', 'inline;'], ['float', 'left;'], ['margin-left', '10px;'], ['margin-right', '10px']]
    - display: 'inline;'
    - float: 'left;'
    - margin-left: '10px;'
    - margin-right: '10px'
  - refs: ['.grid_1', '.grid_2', '.grid_3', '.grid_4', '.grid_5']
[2]:
  ['.featured_container', '.container_12', '.grid_4', 'a', [['color', '#1d1d1d;'], ['float', 'right;'], ['width', '235px;'], ['height', '40px;'], ['text-align', 'center;'], ['line-height', '40px;'], ['border', '4px solid #141a20;']]]
  - attrs: [['color', '#1d1d1d;'], ['float', 'right;'], ['width', '235px;'], ['height', '40px;'], ['text-align', 'center;'], ['line-height', '40px;'], ['border', '4px solid #141a20;']]
    - border: '4px solid #141a20;'
    - color: '#1d1d1d;'
    - float: 'right;'
    - height: '40px;'
    - line-height: '40px;'
    - text-align: 'center;'
    - width: '235px;'
  - refs: ['.featured_container', '.container_12', '.grid_4', 'a']

使用defaultdict(dict)通过引用的CSS对象累积属性:

from collections import defaultdict
accum = defaultdict(dict)
for res in result:
    for name in res.refs:
        accum[name].update(res.attrs)

from pprint import pprint
pprint(accum['.container_12'])

给予:

{'border': '4px solid #141a20;',
 'color': '#1d1d1d;',
 'float': 'right;',
 'height': '40px;',
 'line-height': '40px;',
 'margin-left': 'auto;',
 'margin-right': 'auto;',
 'text-align': 'center;',
 'width': '235px;'}

关于python - 从任意 css 文件中获取特定类的所有 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60398908/

相关文章:

javascript - 大背景图像交换

java - 为什么我的 Android 程序中会出现 PatternSyntaxException?

python - Scrapy 应该在哪个文件/位置处理数据?

python - 类型错误 : unsupported operand type(s) for *: 'int' and 'NoneType'

c# - 如何获取元素的CSS类名称

html - 如何获取 div 动画的当前状态?

Python:如何逐行验证输入文件,修复可能的错误,并将清理过的行写入另一个文件?

正则表达式:紧接着最后一个左括号之后的文本

python - 如何从输出中去掉 unicode 'u'? Python

Python Canvas 比屏幕尺寸大