仅在BeautifulSoup elemen中查找文本

2024-10-02 00:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢


Tags: divonpriceresultsclassspangwssoup
1条回答
网友
1楼 · 发布于 2024-10-02 00:23:04

如果没有文件示例,请尝试:

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,以便只找到每个div的文本。你知道吗

相关问题 更多 >

    热门问题