How can I parse the CSS font shorthand property using Python?

Viewed 70

I am using bs4 and cssutils in Python 3 to gather data on the font properties of text in a set of HTML documents. One of the ways in which font properties are set is using CSS inside a style attribute of an HTML tag. I can easily get this information when the font properties are set using font-size, font-weight, and the like.

However, sometimes the CSS shorthand property font is used in place of those; it has syntax like font: italic small-caps bold 12px/30px Georgia, serif. In that case cssutils gives me only the entire string corresponding to font, not its individual parts. Is there a parser I can use to extract the various properties embedded in the font property, or will I need to do that myself (perhaps with a regular expression to capture font size)? I need to capture at least the font size, style, and weight information. I have searched for but not found CSS parsing tools for Python beyond the cssutils package.

0 Answers
Related