仅查找BeautifulSoup元素内的文本

问题描述 投票:0回答:1

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

谢谢

python selenium web-scraping beautifulsoup
1个回答
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,以便只找到每个人的文本。

© www.soinside.com 2019 - 2024. All rights reserved.