新手求助:通过Riot API获取EUW赛区排位赛数据(Python实现)
1. 用Python获取EUW赛区排位赛数据的实现步骤
你需要用到英雄联盟官方的Riot Games API来获取数据,以下是落地步骤:
前置准备
- 先去Riot官方开发者平台申请个人API密钥(免费额度足够新手测试使用)
- 安装Python依赖库:在终端执行
pip install requests pandas,requests用于发送API请求,pandas方便你处理数据(逻辑和R的数据框类似)
核心代码示例
import requests import pandas as pd from datetime import datetime # 替换成你申请到的API密钥 API_KEY = "你的密钥" # 把日期转成API要求的毫秒级时间戳 date1 = datetime.strptime("2024-01-01", "%Y-%m-%d").timestamp() * 1000 date2 = datetime.strptime("2024-01-02", "%Y-%m-%d").timestamp() * 1000 # 请求头,必须携带API密钥 headers = {"X-Riot-Token": API_KEY} # 第一步:获取EUW赛区单双排排位赛的比赛列表(队列ID=420,灵活组排是440) match_list_url = f"https://euw1.api.riotgames.com/lol/match/v5/matches/by-queue/420?startTime={date1}&endTime={date2}" match_response = requests.get(match_list_url, headers=headers) match_ids = [match["metadata"]["matchId"] for match in match_response.json()["matches"]] # 第二步:遍历每个比赛ID,提取你需要的字段 match_data = [] for match_id in match_ids: match_detail_url = f"https://euw1.api.riotgames.com/lol/match/v5/matches/{match_id}" detail_response = requests.get(match_detail_url, headers=headers) detail = detail_response.json() # 提取两队的英雄和位置 team1_players = detail["info"]["participants"][:5] team2_players = detail["info"]["participants"][5:] team1_hero_pos = [(p["championName"], p["teamPosition"]) for p in team1_players] team2_hero_pos = [(p["championName"], p["teamPosition"]) for p in team2_players] # 提取胜负状态 team1_win = detail["info"]["teams"][0]["win"] team2_win = detail["info"]["teams"][1]["win"] # 整理成字典存入列表 match_data.append({ "team1_heroes": team1_hero_pos, "team2_heroes": team2_hero_pos, "team1_win": team1_win, "team2_win": team2_win }) # 转成DataFrame方便后续分析 df = pd.DataFrame(match_data) print(df.head())
注意事项
- API有请求频率限制,免费额度是每2分钟100次请求,避免一次性批量请求过多数据
teamPosition字段可能存在空值,后续可以用pandas的fillna方法处理- 如果需要其他类型的排位赛,替换队列ID即可(比如灵活组排是440)
2. Python替代R中help()的文档查询方法
Python里有几种和R的help(package)/?package等价的实用方式:
- 内置
help()函数:在Python终端输入help(requests)会输出整个库的文档;查询单个函数用help(requests.get),逻辑和R的?完全一致 - IPython/Jupyter快捷查询:用IPython或Jupyter Notebook时,直接在函数名后加
?,比如requests.get?,会弹出详细文档面板,体验和RStudio的帮助面板相近 - 本地文档服务器:用
pydoc模块启动本地文档服务,终端输入pydoc -b,会自动打开浏览器展示所有已安装库的文档,相当于本地版官方文档 - 查看模块属性:用
dir(requests)可以列出模块内的所有函数、类和属性,类似R的ls()
内容的提问来源于stack exchange,提问作者fatneet
相关产品推荐
相关产品推荐

