[英]Get value of span tag using BeautifulSoup
我有許多 facebook 群組,我想了解其成員的數量。 一個例子是這個組: https : //www.facebook.com/groups/347805588637627/我看過頁面上的檢查元素,它是這樣存儲的:
<span id="count_text">9,413 members</span>
我試圖從頁面中刪除“9,413 名成員”。 我曾嘗試使用 BeautifulSoup,但無法解決。
謝謝
編輯:
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 標簽:
from bs4 import BeautifulSoup
soup = BeautifulSoup(your_html_input, 'html.parser')
span = soup.find("span", id="count_text")
span.text
您可以使用解析跨度的text
屬性:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<span id="count_text">9,413 members</span>', 'html.parser')
>>> soup.span
<span id="count_text">9,413 members</span>
>>> soup.span.text
'9,413 members'
Facebook 使用javascrypt
來防止機器人抓取。 您需要使用selenium
在 python 上提取數據。
如果你有多個 span 標簽,你可以試試這個
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
tags = soup('span')
for tag in tags:
print(tag.contents[0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.