调用Riot API获取Match ID返回200但结果为空的问题排查
问题诊断与修复方案
核心问题分析
1. 账号信息请求的循环逻辑错误
你遍历三个区域时,每次循环都会覆盖global_account_response、global_account_data等变量,最终只保留最后一个区域(asia)的请求结果。如果你的账号实际不在该区域,会导致后续拿到的puuid无效,进而请求对战记录返回空数组;即使账号在其他区域,也会被最后一次无效请求覆盖,触发错误逻辑。
2. 未对游戏名和标签做URL编码
如果游戏名或tagline包含空格、特殊字符(比如#、&),直接拼入URL会导致请求路径无效,可能返回200但实际是错误的账号数据,用错误的puuid请求对战记录自然返回空。
3. 对战记录请求遍历所有区域无意义
每个账号的对战记录只存储在对应的区域路由下(比如美服在americas、欧服在europe、国服在asia),遍历所有区域只会得到两个空数组,只有对应区域的请求能拿到有效数据。
修复后的代码
import requests import urllib.parse if __name__ == "__main__": regional_routing_values = ["americas", "europe", "asia"] api_key = "你的API密钥" game_name = input("\n输入游戏名称\t: ").strip() tagline = input("输入标签\t: #").strip() # 对含特殊字符的游戏名、标签做URL编码 encoded_game_name = urllib.parse.quote(game_name) encoded_tagline = urllib.parse.quote(tagline) headers = {"X-Riot-Token": api_key} valid_account_data = None valid_region = None # 遍历区域,找到存在账号的目标区域后立即停止 for region in regional_routing_values: global_account_url = f"https://{region}.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{encoded_game_name}/{encoded_tagline}" global_account_response = requests.get(global_account_url, headers=headers) if global_account_response.status_code == 200: valid_account_data = global_account_response.json() valid_region = region break if not valid_account_data: print(f"\n召唤师 \"{game_name} #{tagline}\" 不存在于任何区域。") # 打印各区域错误详情用于排查 for region in regional_routing_values: url = f"https://{region}.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{encoded_game_name}/{encoded_tagline}" res = requests.get(url, headers=headers) if res.status_code != 200: err_data = res.json().get('status', {}) print(f"\n区域\t: {region}") print(f"错误码\t: {res.status_code}") print(f"信息\t: {err_data.get('message', '未知错误')}") else: puuid = valid_account_data['puuid'] print(f"\n召唤师 \"{game_name} #{tagline}\" 存在于区域 {valid_region}。") print(f"PUUID : {puuid}\n") # 仅在对应区域请求对战记录 matches_url = f"https://{valid_region}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids?start=0&count=5" matches_response = requests.get(matches_url, headers=headers) matches_data = matches_response.json() print(f"区域\t : {valid_region}") print(f"对战ID : {matches_data}\n")
额外排查点
- 确认API密钥拥有
match-v5和account-v1的调用权限 - 检查目标账号是否在对应区域有对战记录(新账号无对局数据时也会返回空数组)
- 注意Riot API的调用频率限制,避免触发限流导致异常
内容的提问来源于stack exchange,提问作者rshyshyshy
相关产品推荐
相关产品推荐

