Python BeautifulSoup - Div 之间的文本

标签 python beautifulsoup

我正在开发一个网络爬虫项目,无法让 BeautifulSoup 为我提供 Div 之间的文本。下面是我的代码。关于如何让 python 仅打印“5x5”而没有“Div to/Div”且没有空格的任何建议?

source = requests.get('https://www.stor-it.com/self-storage/meridian-id-83646').text
soup = BeautifulSoup(source, 'lxml')
unit = soup.find('div', class_="unit-size")
print (unit)

此脚本返回以下内容:

<div class="unit-size">
                                    5x5                                 </div>

最佳答案

您可以使用文本来检索文本,然后剥离以删除空格 尝试 unit.text.strip()

关于Python BeautifulSoup - Div 之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109284/

相关文章:

python - Pandas:给定特定信号值的前向和后向行计数

python - sklearn 的 Adaboost predict_proba 如何在内部工作?

python - 应该如何编写 Python 文件,使其既可以是模块又可以是具有命令行选项和管道功能的脚本?

python - "Expected string or buffer"使用 Beautiful Soup 时出错

python - BeautifulSoup:抓取嵌入的 href 链接列表

python-3.x - 如何抓取特定表和数据 python 的特定行/单元格

python - 根据 X 绘制 Y,因为 X 是每个 y 值的时间范围

python - 了解每个用户的位置时间 : using the shift function

python - BeautifulSoup 获取具有特定类的 div 中所有 img 的多个元素

python - 为什么 Beautifulsoup 在输入 Mechanize 响应时会返回 NoneTypes?