Find all the span styles with font size larger than the most common one via beautiful soup python

后端 未结 2 1261
刺人心
刺人心 2021-01-28 10:35

I understand how to obtain the text from a specific div or span style from this question: How to find the most common span styles

Now the diff

2条回答
  •  鱼传尺愫
    2021-01-28 11:29

    This may help you:-

        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()
    

提交回复
热议问题