css-parsing

Search cssutils.py parse tree by ID and CLASS selectors


I'm using cssutils in Python to parse CSS style files and would like to search the parse tree by ID and Class. That is, given some CSS style file and some Class name (e.g. div navbar), how do I ascertain the style rules encapsulated by this selector? A solution or any ideas about where to look in the existing documentation (http://packages.python.org/cssutils/index.htm) would be highly appreciated


Solution

  • You can try this out:

    import cssutils
    css_parser = cssutils.CSSParser()
    stylesheet = css_parser.parseUrl(CSS_URL)
    
    for each_rule in stylesheet.CSSRules:
        if ID_NAME in each_rule.selectorText:
            print each_rule.style
    

    Similarly you can try for CLASS_NAME as well.

    Would this work for you?