【问题标题】:Beautifulsoup superscript not "sup"Beautifulsoup 上标不是“sup”
【发布时间】:2018-07-04 11:00:53
【问题描述】:

我可以从以下页面中删除上标:

https://www.sec.gov/Archives/edgar/data/1633917/000163391718000094/exhibit991prq12018pypl.htm

这里有这篇文章:Beautiful soup remove superscripts

但现在我有一个没有标记为sup的上标

https://www.sec.gov/Archives/edgar/data/1549802/000110465918031489/a18-13128_1ex99d1.htm

Net revenues 后面是一个上标1,它没有sup 标记。

如何从此处的帖子中删除此上标:Beautiful soup remove superscripts

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    看起来有问题的元素具有以下格式:

    <font size="1" style="font-size:6.5pt;font-weight:bold;position:relative;top:-3.0pt;">1</font>
    

    所以我们可以在这里看到他们正在使用字体格式化文本,其中重要的部分是样式 position:relativetop: 值。我会亲自编写一个可以扩展的函数来检测上标并删除它们。例如:

    def Remove_Superscripts(soup):
        # Simple superscript extraction
        for element in soup.find_all('sup'):
            element.extract()
    
        # More complex superscript extraction for this example:
        for element in soup.find_all(lambda e: e and e.name == 'font' and e.has_attr('style') and
                                               'position:relative' in e['style'] and
                                               'top:' in e['style']:
            element.extract()
    

    这是一个非常懒惰和混乱的示例,但它应该让您了解如何删除未标记为&lt;sup\&gt; 标记的上标标记。不幸的是,每次遇到有人以不同方式构造上标的新情况时,您都需要扩展和修改此方法(我会努力使其尽可能开放和通用)。

    【讨论】:

      【解决方案2】:

      类似这样的:

      fonts = soup.select('font[style*="position:relative"]')
      for font in fonts:
          font.decompose()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 1970-01-01
        • 2016-01-29
        • 2016-06-04
        • 2011-10-10
        • 2019-03-10
        相关资源
        最近更新 更多