BS4 Beautiful Soup 从 find_all 中提取文本 [英] BS4 Beautiful Soup extract text from find_all

查看:68
本文介绍了BS4 Beautiful Soup 从 find_all 中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取一个网站并想创建一个价格列表.

I am scraping a website and would like to create a list of prices.

prices = soup.find_all("li", class_="price")

然而,这会返回:

 <li class="price">€13.99</li>, 
 <li class="price">€12.99</li>, 
.....

如何只提取价格?我试过

How do I extract just the price? I tried

prices = soup.find_all("li", class_="price", text=True)

但是没有用.

我知道我可以手动浏览列表并提取文本,但这并不理想.

I know I can go through the list manually and extract the text but this isn't ideal.

推荐答案

假设内容不是动态添加的,这似乎不是,我将使用 .text 从使用返回的元素中提取选择

Assuming content is not dynamically added, which it appears it is not, I would use .text to extract from elements returned by using select

prices = [item.text for item in soup.select('li.price')]

这篇关于BS4 Beautiful Soup 从 find_all 中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆