python - 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)
解决方案
在获取文本之前,您可以尝试从汤中提取包含页码的标签。
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.
推荐阅读
- hyperledger-fabric - 无法在 Windows 上的 Hyperledger Fabric 中运行 fabcar 示例
- sql-server - 获取环境值来建模 laravel?
- azure - 是否有一种简单的方法可以了解 Azure 托管标识的工作原理以及它们为何安全?
- php - 数据无法键入数据库
- java - Log4J 不起作用并抛出 ClassNotFoundException
- c++ - 在哪里释放内存而不引起错误
- reactjs - React Router 没有渲染正确的组件
- javascript - Django + Apache + Mobile 发起下载而不是服务页面
- javascript - 忽略 Puppeteer arg “--remote-debugging-port=0”
- python - 如何使用 Try 和 Exception 在 Python 中通过打印消息引发错误?