推荐学习书目
› 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 · Jun 28, 2020 · 4404 views
    This topic created in 2320 days ago, the information mentioned may be changed or developed.

    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 replies  •  2020-06-29 01:41:43 +08:00
    arrow8899
    arrow8899
    Jun 28, 2020
    2
    善用 google
    duan602728596
    duan602728596
    Jun 28, 2020❤️1
    3
    https://astexplorer.net/ 可以去这里看看你需要的
    huyinjie
    huyinjie
    OP
    Jun 28, 2020
    4
    @arrow8899 #2 感谢 之前用了 tinycss 也没能实现 我试下这个
    MarkLeeyun
    MarkLeeyun
    Jun 28, 2020
    5
    哎。用提取 xml 的方法来提取 css?
    jiejiss
    jiejiss
    Jun 28, 2020
    6
    用过一切方法都不行的话就还是 headless chrome 吧。
    huyinjie
    huyinjie
    OP
    Jun 29, 2020
    7
    感谢各位 使用 tinycss2 成功解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2747 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 14:37 · PVG 22:37 · LAX 06:37 · JFK 09:37
    ♥ Do have faith in what you're doing.