【问题标题】:Find all the span styles with font size larger than the most common one via beautiful soup python通过美丽的汤python找到所有字体大小大于最常见的跨度样式
【发布时间】:2017-04-12 02:51:48
【问题描述】:

我了解如何从这个问题中获取特定divspan 样式的文本:How to find the most common span styles

现在的困难是试图找到所有字体大小大于最常见的跨度样式?

我怀疑我应该使用正则表达式,但首先我需要提取特定的最常见的字体大小?

另外,当条件是字符串时,如何确定“大于”?

【问题讨论】:

  • 也许你应该在一个列表中获取所有样式并遍历它并将所有样式的font-size 存储在一个数组中(只是使用正则表达式的数字),你可以找到最常用的数字时间,你也可以找到更大的。

标签: python html beautifulsoup html-table font-size


【解决方案1】:

这可能会对你有所帮助:-

    from bs4 import BeautifulSoup
    import re

    usedFontSize = [] #list of all font number used

    #Find all the span contains style 
    spans = soup.find_all('span',style=True)
    for span in spans:
        #print span['style']
        styleTag = span['style']
        fontSize = re.findall("font-size:(\d+)px",styleTag)
        usedFontSize.append(int(fontSize[0]))

    #Find most commanly used font size
    from collections import Counter
    count = Counter(usedFontSize)
    #Print list of all the font size with it's accurence.
    print count.most_common()

【讨论】:

    【解决方案2】:

    要使用 BeautifulSoup 查找所有字体大小大于最常见的 span 样式的 span 样式,您需要解析每个返回的 CSS 样式。

    解析 CSS 最好使用 cssutils 之类的库。这将让您直接访问fontSize 属性。

    这将有一个值,例如 12px,它不会自然地正确排序。为了解决这个问题,您可以使用诸如 natsort 之类的库。

    所以,首先将每个样式解析为 css 对象。同时保留每个 span 的所有汤的列表,以及样式的解析 CSS。

    现在使用fontSize 属性作为使用 natsort 进行排序的键。这将根据字体大小为您提供正确排序的样式列表,最大优先(使用reverse=True)。然后,takewhile() 用于创建列表中所有条目的列表,直到大小与最常见的条目匹配,从而导致条目列表大于最常见的条目。

    from bs4 import BeautifulSoup
    from collections import Counter
    from itertools import takewhile    
    import cssutils
    import natsort
    
    html = """
        <span style="font-family: ArialMT; font-size:12px">1</span>
        <span style="font-family: ArialMT; font-size:14px">2</span>
        <span style="font-family: ArialMT; font-size:1px">3</span>
        <span style="font-family: Arial; font-size:12px">4</span>
        <span style="font-family: ArialMT; font-size:18px">5</span>
        <span style="font-family: ArialMT; font-size:15px">6</span>
        <span style="font-family: ArialMT; font-size:12px">7</span>
        """
    
    soup = BeautifulSoup(html, "html.parser")    
    style_counts = Counter()
    parsed_css_style = []       # Holds list of tuples (css_style, span)
    
    for span in soup.find_all('span', style=True):
        style_counts[span['style']] += 1
        parsed_css_style.append((cssutils.parseStyle(span['style']), span))
    
    most_common_style = style_counts.most_common(1)[0][0]
    most_common_css_style = cssutils.parseStyle(most_common_style)
    css_styles = natsort.natsorted(parsed_css_style, key=lambda x: x[0].fontSize, reverse=True)
    
    print "Styles larger than most common font size of {} are:".format(most_common_css_style.fontSize)
    
    for css_style, span in takewhile(lambda x: x[0].fontSize != most_common_css_style.fontSize, css_styles):
        print "  Font size: {:5}  Text: {}".format(css_style.fontSize, span.text)
    

    在显示的示例中,最常用的字体大小是12px,因此还有3个大于此的其他条目如下:

    Styles larger than most common font size of 12px are:
      Font size: 18px   Text: 5
      Font size: 15px   Text: 6
      Font size: 14px   Text: 2
    

    要安装,您可能需要:

    pip install natsort
    pip install cssutils    
    

    请注意,这确实假设您网站上使用的字体大小是一致的,它无法比较不同的字体指标,只能比较数值。

    【讨论】:

    • 谢谢Martin,我还在测试中,因为我需要使用font-size信息回soup函数来捕获内容。首先只是一个小问题,为什么 != 给出大于?应该不等于吧?顺便说一句,不知道为什么有人点击没有帮助.....
    • 此时的列表已经按照从大到小排序,因此takewhile 用于从完整列表中读取条目,直到与12px 条目匹配为止。
    • 感谢马丁,我尝试使用正则表达式将字体大小恢复到汤 row.get 函数,但它给出了这个错误: AttributeError: 'ResultSet' object has no attribute 'get '
    • 抱歉,有几天没靠近电脑了。如果您需要给定条目的关联汤,则只需在获取 stykes 的同时存储它。有机会我会更新的。
    • 我已经更新了脚本,让您可以访问每个跨度条目的soup
    猜你喜欢
    • 2021-07-25
    • 2017-04-07
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多