代码之家  ›  专栏  ›  技术社区  ›  jason

美化组删除底部的页码

  •  0
  • jason  · 技术社区  · 8 年前

    我正试图从这个html中删除页码。似乎是按照这个模式 '\n','number','\n' 如果你看看名单 texts

    import requests
    from bs4 import BeautifulSoup
    from bs4.element import Comment
    
    def tag_visible(element):
        if element.parent.name in ['sup']:
            return False
        if isinstance(element, Comment):
            return False
        return True
    
    url='https://www.sec.gov/Archives/edgar/data/1318605/000156459018019254/tsla-10q_20180630.htm'
    html = requests.get(url) 
    
    soup = BeautifulSoup(html.text, 'html.parser')
    texts = soup.findAll(text=True)
    
    ### could remove  ['\n','number','\n']
    
    visible_texts = filter(tag_visible, texts)  
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Andrej Kesely    8 年前

    在获取文本之前,可以尝试从soup中提取包含页码的标记。

    soup = BeautifulSoup(html.text, 'html.parser')
    
    for hr in soup.select('hr'):
        hr.find_previous('p').extract()
    
    texts = soup.findAll(text=True)
    

    <p style="text-align:center;margin-top:12pt;margin-bottom:0pt;text-indent:0%;font-size:10pt;font-family:Times New Roman;font-weight:normal;font-style:normal;text-transform:none;font-variant: normal;">57</p>
    <p style="text-align:center;margin-top:12pt;margin-bottom:0pt;text-indent:0%;font-size:10pt;font-family:Times New Roman;font-weight:normal;font-style:normal;text-transform:none;font-variant: normal;">58</p>
    
    ... etc.