python - 仅查找 BeautifulSoup 元素内的文本
问题描述
我运行这个 python BS 代码:
soup=BeautifulSoup(wd.page_source, 'lxml')
price_divs = soup.find_all("div", class_="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price")
print(price_divs)
这个输出:
<div class="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price">
<span class="gws-flights-results__carry-on-definitely-not-included gws-flights-results__marker" jsaction="LoTHjf;mouseenter:LoTHjf;mouseleave:QsRKXb" role="button" tabindex="-1"></span> €105</div>
<div class="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price">
<span class="gws-flights-results__carry-on-definitely-not-included gws-flights-results__marker" jsaction="LoTHjf;mouseenter:LoTHjf;mouseleave:QsRKXb" role="button" tabindex="-1"></span> €105</div>
<div class="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price">€107</div>
<div class="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price"> €107</div>
我希望这只是给我一个所有价格的数组,例如:
[105,107]
谢谢
解决方案
如果没有文件样本,请尝试:
soup=BeautifulSoup(wd.page_source, 'lxml')
price_divs = soup.find_all("div", class_="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price")
for price in price_divs:
print(price.text)
为什么:
遍历 div 以便仅查找每个人的文本。
推荐阅读
- next.js - Applo 客户端单独或 Next js 单独可以在 Next+Applo 应用程序中进行 SSR 吗?
- c# - 有没有办法在套接字接收中引入分隔符?
- jira - jira - JQL 显示问题的所有子项
- python - __init__ 方法 python 中的空列表
- python - 在 python 项目中使用 PyCharm 中现有的 MySQL 连接而无需再次手动连接
- macos - 无法打开“”进行写入——路径上的共享违规。外置硬盘
- python - 使用 pandas 进行数据转换
- python - 将 Raspberry Pi 中的传感器数据生成到 AWS IoT
- javascript - 尝试在节点中删除和创建文件夹
- firebase - 如何在 firebase 函数中实现 Firebase AppCheck 以读取实时数据库?