python - 使用 python 从 HTML 中获取文本
问题描述
我有 HTML 数据,我想获取
标签并将其放入数据框中以进行进一步处理。
但我只想要文本中的
这些标签之间的标签:
<div class="someclass" itemprop="text">
<p>some text</p>
</div>
使用 BeautifulSoup 我可以在所有
标签很容易。但正如我所说,我不想要它,除非它在这些标签之间。
解决方案
如果想要标签中的文本仅与特定类相关联,则可以使用 BeautifulSoup 指定具有以下attrs
属性的特定类:
html = '''<div class="someclass" itemprop="text">
<p>some text</p>
</div>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
tags = soup.find_all('div', attrs={'class': 'someclass'})
for tag in tags:
print(tag.text.strip())
输出:
some text
推荐阅读
- function - Lua - 传递一个已经填充了一些参数的函数
- spring-boot - Spring Integration:通过配置连接到多个 MQ 服务器
- python - 我需要根据计数过滤并在 Python 中返回值
- ios - glCopyTexImage2d 的金属等效物
- node.js - 如何使 id 的约束不同
- r - 如何在 R(闪亮)中的“BOX()”中查看反应部分的文本
- javascript - 将多选与动态生成的选择一起使用
- html - 在 Angular 中显示材料表
- python-3.x - Python - API 多个响应
- jsp - 在 JSP 中禁用复选框时显示消息