【问题标题】:Find only text inside BeautifulSoup element仅查找 BeautifulSoup 元素内的文本
【发布时间】:2019-02-21 11:17:23
【问题描述】:

我运行这个 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]

谢谢

【问题讨论】:

  • 张贴你的样本lxmlwd.page_source

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

如果没有您的文件样本,请尝试:

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 以便仅查找每个文本。

【讨论】:

  • 提示:对于这个问题的其他旁观者,​​请始终寻找与您需要的标签相同的唯一非动态属性。
猜你喜欢
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 2022-01-05
  • 2021-12-27
  • 2013-01-21
  • 2012-01-25
相关资源
最近更新 更多