为什么从某个网站上删除数据很困难



我一直在努力学习一些关于python自动化的知识,在其中我试图学习web报废。我选择制作一个discord机器人,它接受一个名为"游戏"的玩家的用户id;brawlhalla";并从一些实时跟踪网站上显示玩家的详细信息我的方法是接受用户的播放代码,然后前往https://brawlhallastats.herokuapp.com/player/?player="用户ID";使用熊猫或美人汤这样的图书馆

但真正的问题是,当我使用pandas从网站上提取数据时,绝对不会检测到任何表,当我尝试使用beautifulSoup时,它现在显示的是数据

感觉他们试图以某种方式保护数据。。。。。。。。。。。。。

我建议运行此脚本并输入代码:65340087

import requests
from bs4 import BeautifulSoup
#importing modules 
code = input("enter the code :")
url = "https://brawlhallastats.herokuapp.com/player/?player="+code 
print(url)
r = requests.get(url)
htmlContent = r.content
soup = BeautifulSoup(htmlContent,'html.parser')
#getting the username but not working
playername = soup.find('span',{'id':'playerName'})
print(playername)
#getting the playerlever but not working
playerLevel = soup.find('span',{'id':'playerLevel'})
print(playerLevel)

输出将是:

https://brawlhallastats.herokuapp.com/player/?player=65340087
<span id="playerName" style="text-shadow: 1px 1px #eee; color:#AAAAAA"> RupRep444</span>
<span id="playerLevel"><b>Level:</b> 32.85<br/></span>

但如果你前往网址:https://brawlhallastats.herokuapp.com/player/?player=65340087你会注意到playerName是另一个东西";HetoskiWannaWeed";玩家等级为:58

关于这个问题,我需要一个非常好的帮助,因为这是我试图构建的第一个实时项目,并且正在与的大量问题作斗争

您在页面上看到的数据是用JavaScript加载的,所以beautifulsoup看不到它。解决方案可以使用他们的Ajax URL,并使用requests:请求Json数据

import json
import requests
player_id = 65340087
api_url = (
"https://brawlhallastats.herokuapp.com/api/submit-form3-by-id?player={}"
)
data = requests.get(api_url.format(player_id)).json()
# uncomment to print all data:
# print(json.dumps(data, indent=4))
print(data["name"], data["level"])

打印:

HetoskiWannaWeed 58

最新更新