1 回答
TA贡献1843条经验 获得超7个赞
基本上页面是通过加载的JavaScript
,所以bs4
或requests
模块将无法JavaScript
动态渲染。
你应该使用selenium
或requests_html
模块来渲染JS
,但我注意到网站正在使用API
,它可以用来获取数据,所以我调用了它并提取了数据。
import requests
import pandas as pd
params = {
"Counter": "1000",
"DateFrom": "",
"DateTo": "",
"Direction": "DESC",
"LeagueID": "00",
"PlayerOrTeam": "P",
"Season": "2018-19",
"SeasonType": "Regular Season",
"Sorter": "DATE"
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
"x-nba-stats-origin": "stats",
"x-nba-stats-token": "true",
"Referer": "https://stats.nba.com/players/boxscores/?Season=2018-19&SeasonType=Regular%20Season"
}
def main(url):
r = requests.get(url, params=params, headers=headers).json()
goal = []
for item in r['resultSets']:
df = pd.DataFrame(item['rowSet'], columns=item['headers'])
goal.append(df)
new = pd.concat(goal)
print(new)
new.to_csv("data.csv", index=False)
main("https://stats.nba.com/stats/leaguegamelog")
输出:在线查看
- 1 回答
- 0 关注
- 112 浏览
添加回答
举报