python dota2数据 1 API
2017-11-17 11:04
477 查看
python dota2数据 1 比赛历史
注册API Key
https://steamcommunity.com/dev/apikey安装dota2api
使用python的dota2api库来调用API:pip3 install dota2api
参考文档
http://dota2api.readthedocs.io/en/latest/installation.html获取最近5场使用英雄ID
添加API KEY:
在linux中:export D2_API_KEY=xxxxx
也可在python文件中初始化时将KEY作为dota2api.Initialise()的参数。
查找API
查找相关函数发现查询比赛用到的函数为get_match_history(),其参数为:account_id 包含玩家ID
hero_id 查询的对应英雄的比赛,每个英雄对应一个ID
game_mode 游戏模式
skill 水平等级
min_players 最少玩家数量
league_id 所属联赛ID
start_at_match_id 起始比赛ID
matches_requested 查询数量,默认为100
tournament_games_only 是否只包含联赛比赛
所有参数均为可选。
在这里我们需要用到的仅有account_id和matches_requested。
再查看get_matches_history()函数的返回内容:
{ num_results - Number of matches within a single response total_results - Total number of matches for this query results_remaining - Number of matches remaining to be retrieved with subsequent API calls [matches] - List of matches for this response { match_id - Unique match ID match_seq_num - Number indicating position in which this match was recorded start_time - Unix timestamp of beginning of match lobby_type - See lobby_type table [player] - List of players in the match { account_id - Unique account ID player_slot - Player's position within the team hero_id - Unique hero ID } } }
返回的是一个dictionary对象,其中最主要的是matches键,该值为一个list对象,其中包含了所查询的所有比赛信息。解析得到每一场比赛对应的dictionary,再进一步解析得到比赛的详细信息,如比赛ID,所有玩家ID和所用英雄ID。
其中matches中的players在原文档中显示为player,经测试应为players。
get_match_history.py
import dota2api import sys #初始化api api = dota2api.Initialise() #接收命令行参数 #我的dota2用户ID my_id = sys.argv[1] #请求的比赛场数 matches = sys.argv[2] #调用get_match_history函数 match_history = api.get_match_history(account_id = my_id, matches_requested = matches) #解析获得的match_history字典对象,取其中的matches键 matches = match_history['matches'] #逐个解析每场比赛 for m in matches: #获取比赛ID m_id = m['match_id'] #获取所有玩家 players = m['players'] #逐个解析每个玩家 for p in players: #寻找自己 if p['account_id'] == int(my_id): #获取所用英雄ID h_id = p['hero_id'] break print('match id: ', m_id, 'hero: ', h_id)
相关文章推荐
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- API例子:用Python驱动Firefox采集网页数据
- Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API
- python利用新浪API实现数据的抓取\python微博数据爬虫
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- API例子:用Python驱动Firefox采集网页数据
- python MNIST手写识别数据调用API的方法
- Python3--批量爬取数据之调用百度api进行翻译
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 大数据学习[13]:elasticsearch之简单的python API
- 使用python及百度API对百度poi数据进行爬取(三)
- python 调用航空公司的接口 获取机票数据 api简单案例
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- 使用python及百度API对百度poi数据进行爬取(一)
- API例子:用Python驱动Firefox采集网页数据
- 用python抓取摩拜单车API数据
- 利用python调用elasticsearch-api来分析数据并作图进行日报邮件发送
- Python通过Zabbix API获得数据
- 如何采用Python zabbix_api 获取性能数据
- Python3--批量爬取数据之调金山词霸api进行翻译