作者: 锋小刀
微信搜索【Python与Excel之交】关注我的公众号查看更多内容
前言
NBA由北美三十支队伍组成的男子职业篮球联盟,汇集了世界上最顶级的球员,是美国四大职业体育联盟之一。
本文爬取了NBA中国官方网站球员信息,进行数据可视化分析。
数据获取
网站数据存放在json格式的链接中,在往期文章中已详细讲解过如何获取,本文就不做赘述,以下给出完整代码:
# -*-codEing = utf-8 -*-
# @Time : 2021/1/28 13:56
# @Author : 公众号 职场办公杂技
# @File : NBA球员数据获取.PY
# @Software : PyCharm
import requests
import pandas as pd
df = pd.DataFrame()
response = requests.get('https://china.nba.com/static/data/league/playerlist.json').json()
for playerProfile in response['payload']['players']:
displayName = playerProfile['playerProfile']['displayName'] # 中文名称
displayNameEn = playerProfile['playerProfile']['displayNameEn'] # 英文名称
displayAbbr = playerProfile['teamProfile']['displayAbbr'] # 球队
position = playerProfile['playerProfile']['position'] # 位置
height = playerProfile['playerProfile']['height'] # 身高
weight = playerProfile['playerProfile']['weight'] # 体重
experience = playerProfile['playerProfile']['experience'] # 经验
country = playerProfile['playerProfile']['country'] # 国籍
print(displayName, displayNameEn, displayAbbr, position, height, weight, experience, country, sep=' | ')
data = pd.DataFrame({
'中文名称': [displayName], '英文名称': [displayNameEn], '球队': [displayAbbr], '位置': [position], '身高': [height], '体重': [weight], '经验': [experience], '国籍': [country]})
df = pd.concat([df, data])
df.to_csv