V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
huyinjie
V2EX  ›  Python

请问如何用 Python 解析 CSS 文件

  •  
  •   huyinjie · 2020-06-28 20:59:13 +08:00 · 3070 次点击
    这是一个创建于 1369 天前的主题,其中的信息可能已经有所发展或是发生改变。

    CSS 文件类似: https://pastebin.com/QVdRQzPJ
    想要提取 Class 值和对应 font-family 值,以及 font-family 值和对应 Url 。试了下使用 cssutils 库只能提取到 font-family 值,无法获得 Class 值。
    请问还有什么别的方法吗

    sheet = cssutils.parseString(cssText)
    for rule in sheet:
        for property in rule.style:
            print(property.name)
            print(property.value)
    
    7 条回复    2020-06-29 01:41:43 +08:00
    arrow8899
        1
    arrow8899  
       2020-06-28 21:10:17 +08:00   ❤️ 1
    arrow8899
        2
    arrow8899  
       2020-06-28 21:10:55 +08:00
    善用 google
    duan602728596
        3
    duan602728596  
       2020-06-28 21:17:31 +08:00   ❤️ 1
    https://astexplorer.net/ 可以去这里看看你需要的
    huyinjie
        4
    huyinjie  
    OP
       2020-06-28 21:30:27 +08:00
    @arrow8899 #2 感谢 之前用了 tinycss 也没能实现 我试下这个
    MarkLeeyun
        5
    MarkLeeyun  
       2020-06-28 21:33:38 +08:00
    哎。用提取 xml 的方法来提取 css?
    jiejiss
        6
    jiejiss  
       2020-06-28 22:22:46 +08:00
    用过一切方法都不行的话就还是 headless chrome 吧。
    huyinjie
        7
    huyinjie  
    OP
       2020-06-29 01:41:43 +08:00
    感谢各位 使用 tinycss2 成功解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5357 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:53 · PVG 14:53 · LAX 23:53 · JFK 02:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.