使用BeautifulSoup获取span标签的值 [英] Get value of span tag using BeautifulSoup

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

问题描述

我有很多Facebook团体,我希望得到其会员人数.该组就是一个例子: https://www.facebook.com/groups/347805588637627/ 我已经查看了页面上的inspect元素,它的存储方式如下:

I have a number of facebook groups that I would like to get the count of the members of. An example would be this group: https://www.facebook.com/groups/347805588637627/ I have looked at inspect element on the page and it is stored like so:

<span id="count_text">9,413 members</span>

我正试图从页面中吸引"9,413名成员".我曾尝试使用BeautifulSoup,但无法解决问题.

I am trying to get "9,413 members" out of the page. I have tried using BeautifulSoup but cannot work it out.

谢谢

from bs4 import BeautifulSoup
import requests

url = "https://www.facebook.com/groups/347805588637627/"
r  = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "html.parser")
span = soup.find("span", id="count_text")
print(span.text)

推荐答案

如果页面中有多个span标签:

In case there is more than one span tag in the page:

from bs4 import BeautifulSoup
soup = BeautifulSoup(your_html_input, 'html.parser')
span = soup.find("span", id="count_text")
span.text

这篇关于使用BeautifulSoup获取span标签的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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