使用BS4从具有某些属性的某些标签中获取内容 [英] Get content from certain tags with certain attributes using BS4

查看:32
本文介绍了使用BS4从具有某些属性的某些标签中获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从具有这些属性的以下标记中获取内容:< span class ="h6 m-0"" .我将遇到的HTML的示例是< span class ="h6 m-0-"> Hello world</span> ,显然,它需要返回 Hello world.

I need to get the content from the following tag with these attributes: <span class="h6 m-0">. An example of the HTML I'll encounter would be <span class="h6 m-0">Hello world</span>, and it obviously needs to return Hello world.

我当前的代码如下:

page = BeautifulSoup(text, 'html.parser')
names = [item["class"] for item in page.find_all('span')]

这可以很好地工作,并且可以让我获得页面中的所有跨度,但是我不知道如何指定我只想要具有特定类"h6 m-0" 和抓取里面的内容.我将如何去做?

This works fine, and gets me all the spans in the page, but I don't know how to specify that I only want those with the specific class "h6 m-0" and grab the content inside. How will I go about doing this?

推荐答案

page = BeautifulSoup(text, 'html.parser')
names = page.find_all('span' , class_ = 'h6 m-0')

在不知道您的用例的情况下,我不知道这是否行得通.

Without knowing your use case I don't know if this will work.

这篇关于使用BS4从具有某些属性的某些标签中获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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