【问题标题】:CSS parsing Python for finding font-family [duplicate]CSS解析Python以查找字体系列[重复]
【发布时间】:2013-05-22 17:56:31
【问题描述】:

给定一个样式表文件。我想找出整个样式表中使用的字体系列值。请问有人可以提示我这样做的想法吗?

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

对不起,如果这是一个菜鸟问题。只是愿意学习。

【问题讨论】:

  • 我的回答也适用于这里;使用 CSS 解析器,然后查询 font-family 规则的结果。

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


【解决方案1】:

试试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

希望对您有所帮助。

【讨论】:

  • 非常感谢您考虑帮助我。 alecxe 和 Martijn。
猜你喜欢
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2019-01-15
  • 2016-06-01
相关资源
最近更新 更多