在BeautifulSoup元素内仅查找文本

时间:2019-02-21 11:17:24

标签: python selenium web-scraping 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]

谢谢

1 个答案:

答案 0 :(得分: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以便仅查找每个人的文本。