使用Beautiful Soup和Requests提取数据

时间:2018-12-29 16:15:36

标签: python beautifulsoup python-requests

我正在尝试使用Python中的Beautiful souprequests包从Stackoverflow抓取数据。我已经能够提取大多数细节,但是当我尝试提取用户的信誉分数时,我只能提取reputation scoreGold的数据,而不能提取有关SilverBronze计数。

以下是我用来提取的代码:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://stackoverflow.com/questions/53968946/how-can-i-limit-function-slot-play-just-for-5-turn-with-do-while-loop').text
soup = BeautifulSoup(source,'lxml')
article = soup.find('div', class_='inner-content clearfix')
user_reputation_score = article.find('span', class_='reputation-score').text
print(user_reputation_score)

金牌代码:

gold_badge = article.find('div', class_='-flair').find('span', class_='badgecount').text
print(gold_badge)

思考如何扩展上述内容以提取白银和青铜批次的数据。

使用以下链接进行测试:

https://stackoverflow.com/questions/53968946/how-can-i-limit-function-slot-play-just-for-5-turn-with-do-while-loop

请注意,我这样做纯粹是出于教育目的。谢谢。

1 个答案:

答案 0 :(得分:1)

find()返回第一个元素,使用find_all()

badge = article.find('div', class_='-flair').find_all('span', class_='badgecount')
gold_badge = badge[0].text
silver_badge = badge[1].text
bronze_badge = badge[2].text
print(gold_badge, silver_badge, bronze_badge) # 2 7 26