首页 > 解决方案 > Beautifulsoup 删除底部的页码

问题描述

我正在尝试从此 html 中删除页码。'\n','number','\n'如果您查看列表,它似乎遵循模式texts。我能用 BeautifulSoup 做吗?如果没有,我如何从列表中删除该模式?

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)  

标签: pythonlistbeautifulsoup

解决方案


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

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.

推荐阅读