[英]Using find_all to find a certain class in BS4 with BeautifulSoup
我试图从这个网站找到所有顶级玩家“竞技场点数”(游戏中的竞争点系统): https://www.epicgames.com/fortnite/competitive/en-US/hype-leaderboard?sessionInvalidated=true
://www.epicgames.com/fortnite/competitive/en-US/hype-leaderboard?sessionInvalidated https://www.epicgames.com/fortnite/competitive/en-US/hype-leaderboard?sessionInvalidated=true
我正在使用请求来获取信息并使用 bs4 对其进行排序,但是我在使用 bs4 时遇到了问题。 这是我到目前为止所拥有的:
page = requests.get('https://www.epicgames.com/fortnite/competitive/en-US/hype-leaderboard?sessionInvalidated=true')
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find_all('td', class_ = "asdt-points")
print(len(results))
运行时,该程序输出 0,这意味着它没有找到任何东西......通过在线研究我没有找到任何可以帮助我的开创性信息,我使用堆栈作为最后的手段。 任何帮助都会很棒! 谢谢!
您将无法使用请求库来执行此操作,因为该页面正在向 api 询问这些统计信息,这些统计信息可以在浏览器控制台的“网络”选项卡中找到
import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.epicgames.com/fortnite/competitive/en-US/hype-leaderboard?sessionInvalidated=true')
soup = BeautifulSoup(page.content,features="lxml")
soup.find("div", {"class": "ApiStandingsDetailsTables"}).text
'Loading'
所以相反让我们问问 api
import requests
api_url='https://www.epicgames.com/fortnite/competitive/api/leaderboard/persistent/Hype_S14/undefined'
data_to_parse=requests.get(api_url).json()
resulting_list_of_dictionaries=[{'rank':i.get('rank'), 'name':i.get('players')[0]['displayName'], 'hype':i.get('pointsEarned')} for i in data_to_parse['entries']]
resulting_list_of_dictionaries
00:{'hype': 75434, 'name': 'Marzz_Ow', 'rank': 1}
01:{'hype': 50160, 'name': 'Its Filipе', 'rank': 2}
02:{'hype': 45821, 'name': 'たいちゃん0216', 'rank': 3}
03:{'hype': 42827, 'name': 'Fishy R2L2', 'rank': 4}
04:{'hype': 41806, 'name': 'Claw FA', 'rank': 5}
05:{'hype': 41379, 'name': '必ずアニキ救って来いやァァァァァ', 'rank': 6}
06:{'hype': 40511, 'name': 'えすぼん.', 'rank': 7}
07:{'hype': 39566, 'name': 'LG Slackes', 'rank': 8}
这是一个需要使用 selenium 的问题,因为该表是使用 Javascript 动态加载的。 如果数据已经加载,Bs4 只能工作,并且可以在您的情况下工作。 发生的事情是,当请求获取页面时,确实没有 td.asdt-points 元素。 你可以看到通过使用:
print(page.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.