CSS解析Python以查找font-family

时间:2013-05-27 21:10:25

标签: python css web-scraping beautifulsoup web-crawler

给出样式表文件。我想找出样式表中使用的font-family值。请有人暗示我这样想吗?

我抓取了,使用Beautifulsoup解析了样式表链接。但是现在我留下了一大串样式表。

很抱歉,如果这是一个菜鸟问题。只是愿意学习。

1 个答案:

答案 0 :(得分:2)

尝试cssutils套餐,例如:

import cssutils


data = """
p{font-family:"Verdana"}

p{font-family:"Comic Sans"}

p{font-family:"Times New Roman", Times, serif}
"""

sheet = cssutils.parseString(data)

for rule in sheet:
    if rule.type == rule.STYLE_RULE:
        # find property
        for property in rule.style:
            if property.name == 'font-family':
                print property.value

打印:

"Verdana"
"Comic Sans"
"Times New Roman", Times, serif

另外,请参阅Martijn的回答:BeautifulSoup: get css classes from html

希望有所帮助。