使用Python解析DOM以提取数据

2024-05-15 01:22:48 发布

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

我有以下代码输出从<div>标记提取的数据

s = BeautifulSoup(driver.page_source, "lxml")

best_price_tags = s.findAll('div', "flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price")
best_prices = []
for tag in best_price_tags:
    best_prices.append(tag.text.replace('€', '').strip())

变量best_price_tags的第一个元素包含以下内容:

<div class="flt-subhead1 gws-flights-results__price gws-flights-results__cheapest-price">      1 820 €   </div>

我期望上述代码只输出值1821

上述代码块在输出下列情况时存在问题,考虑^ {< CD3>},^ {< CD4>}。

我尝试了以下方法,但不幸的是没有成功

for tag in best_price_tags:
    best_prices.append(int(tag.text.replace('€', '').strip()))

寻找不使用NLP模块的自动化解决方案

注意:我已经编辑了<div>标记的确切值。过去是<div class='...'>1 820 €</div>,现在是<div class='...'> 1 820 € </div>


Tags: 代码标记divtagtagspriceresultsclass

热门问题