python dota2數據 3 下載勝負數據
目標:下載每場比賽獲勝方或戰敗方的5個英雄,用於後續分析。
由於get_match_history()返回的內容中並不包括比賽勝負,還需要對每一場比賽使用get_match_details()查詢,效率較低。
使用另一個查詢比賽的API,get_match_history_by_seq_num():
{
status
1 - Success
8 - Matches_requested must be greater than 0
statusDetail - Message explaining a status that is not equal to 1
[matches] - See get_match_details()
}
該函數可直接查詢從指定sequence_id開始的比賽,返回的內容包含了get_match_details()中的比賽詳細信息。(sequence_id與match_id不同)
取起始比賽sequence_id爲3100000000,經查詢爲2017年11月16日,7.07版本發佈後幾天。
將該數字存在一個文件latest.dat中,用於每次查詢前後讀取和更新,便於中斷和繼續下載。
獲取數據後對其進行處理,使每一行先存儲獲勝方5個英雄的ID,再存儲戰敗方5個英雄的ID,最後爲比賽的sequence_id,以這種形式存儲在matches.dat中。
import dota2api
import time
api = dota2api.Initialise()
counter = 0
while 1:
#讀取已下載的最新比賽的sequence_id
f_latest = open('latest.dat', 'r')
latest = int(f_latest.readline())
latest += 1
f_latest.close()
#調用API獲取比賽信息
while(1):
try:
data_got = api.get_match_history_by_seq_num(matches_requested = 50, start_at_match_seq_num = latest)
except (Exception):
print('error, wait 3 seconds.******************************************')
time.sleep(3)
continue
else:
break
matches = data_got['matches']
#寫入文件
f_data = open('matches.dat', 'a')
#對獲取的每場比賽
for m in matches:
#比賽ID
id = m['match_id']
heroes = []
#獲勝方
win = m['radiant_win']
#所有玩家
players = m['players']
#排除人數不爲10的,如solo
if len(players) != 10:
print('player num error: ', len(players))
continue
#獲取所有上場英雄
for p in players:
hero_id = p['hero_id']
heroes.append(hero_id)
#天輝英雄
radiant_team = heroes[0:5]
#夜魘英雄
dire_team = heroes[5:10]
#按ID排序
radiant_team.sort()
dire_team.sort()
#排除英雄ID錯誤
if radiant_team[0] == 0:
print('player hero error: 0')
continue
if dire_team[0] == 0:
print('player hero error: 0')
continue
#按獲勝方和戰敗方存儲英雄ID
if win:
win_team = radiant_team
lose_team = dire_team
else:
win_team = dire_team
lose_team = radiant_team
#當前比賽的sequence_id
latest = m['match_seq_num']
counter += 1
print(counter, id, win, win_team, lose_team, latest)
#寫入文件
for h in win_team:
f_data.write(str(h) + ' ')
for h in lose_team:
f_data.write(str(h) + ' ')
f_data.write(str(latest))
f_data.write('\n')
#更新latest.dat
f_update = open('latest.dat', 'w')
f_update.writelines(str(latest))
f_update.close()
f_data.close()
運行後的matches.dat: