Python tells you ti8 dota2 hero bp

Source: Internet
Author: User
Tags parse string dota

Article Link: https://mp.weixin.qq.com/s/phJzZEQojndY-iNe77RF_w

Congratulations OG become ti8 champion, unfortunately this even year Ti8 Chinese team LGD and champion missed.

School was a DotA player, now no play, for this international event will be more concerned about, this article is to crawl Ti8 period, the BP hero statistics (b is Ban's abbreviation: Prohibit a hero to play, p is the abbreviation of pick: Select Heroes to play).

Python crawlers have written several articles before, all using requests+ BeautifulSoup for page parsing to obtain data.

Python itchat Crawl friend information

Python crawler learning: Crawler QQ to say and generate word cloud, memories full

As always, found such a page, but it does not parse the data.

A closer look, the original data is JS dynamic loading rendering, you can see the interface request to the data, fortunately, this interface does not require the authentication information such as cookies, the direct GET request can get data, so it is convenient to handle, the control page can know the JSON format of the field names.

JSON structure analysis

The data source is the interface, directly

response = requests.get(url)data = json.loads(response.text)

Json.loads to parse String type data, the main data format is as follows:

  {total:402, data: [{match_id:4080856812, radiant: {team_id:15, Name: "PSG. LGD ", Tag:" PSG.                LGD ", Country_code:" CN ", Score:34, bans: [{//each Hero Data name:" Spectre ", NAME_CN: "Ghost", id:67}, ...], picks: [{name: "Earthshak        Er ", NAME_CN:" Shake the Ground ", id:7}, ...]             }, Dire: {team_id:2586976, Name: "og", Tag: "og", Country_code: "US", SCORE:35, bans: [{name: "Tiny", Name_cn: "Little", id:1  9}, ...], picks: [{name: "Furion", Name_cn: "Prophet", ID:        53}, ...] }, radiant_win:0, End_time: "2018-08-26 10:51"}, ...]}  

Each item contains radiant, Dire, which radiant_win: 1, represents Radiant wins, and 0 represents dire wins. Bans inside is Ban's hero data list, picks inside is pick Hero data list.

Here is to use the loop constantly to request to obtain, each time the data is 20, you can change the size of the number of bars.

page = 1while True:    url = "https://www.dotamore.com/api/v1/league/matchlist?league_id=9870&page=%d&size=20" % page    response = requests.get(url)    data = json.loads(response.text)    page += 1    for item in data["data"]:        # 比赛从8月16开始,小于这个时间生成excel,跳出循环        if item["end_time"] < "018-08-16 00:00":            //生成excel            ...        return        //bp数据        ...
BP data

Each of the bans, picks data to be processed, each hero is a record, repeat + 1, so give each hero count of attributes, record the number of times.

# item 指radiant 或dire 的bans、picks列表数据def bp(item, bp_dict):    if item is None:        return    # 遍历bans 或picks 数据    for i, bp in enumerate(item):        key = bp["name"]        # 如果这个英雄已存在,count+1        if key in bp_dict.keys():            bp_dict[key]["count"] = bp_dict[key]["count"] + 1        else:  # 不存在就记录一条数据            bp_dict[key] = copy.deepcopy(bp)            bp_dict[key].update(count=1)    return bp_dict

Each hero data is a key-value pair dictionary, the key is the name attribute is the English name, the value is bans, picks in the hero data, by the way, plus the Count property. Each hero data exists dict ()

{ //每条英雄数据    name: "tiny",    name_cn: "小小",    id: 19,    count:1}

Get two teams of BP data stored separately

b_dict = dict()p_dict = dict()# ban的数据bp(item["radiant"]["bans"], b_dict)bp(item["dire"]["bans"], b_dict)# pick的数据bp(item["radiant"]["picks"], p_dict)bp(item["dire"]["picks"], p_dict)

You can also count the number of appearances of all heroes, not bp_list.
We can also get the BP situation of the championship team, TEAM_ID is the ID of the team.

if item["radiant_win"] == 0:    if item["dire"]["team_id"] == "2586976":        bp(item["dire"]["bans"], b_win_dict)        bp(item["dire"]["picks"], p_win_dict)else:    if item["radiant"]["team_id"] == "2586976":        bp(item["radiant"]["bans"], b_win_dict)        bp(item["radiant"]["picks"], p_win_dict)
Build Excel

The above data we get is a dictionary, there are the heroes of the BP number, now to sort these data, by the number of times from large to small sort, here is the sorted() method

# x[0]是根据键排序,x[1]是根据值,这里的值是字典,取["count"]项排序,得到的是元祖的listnew_b_dict = sorted(b_dict.items(), key=lambda x: x[1]["count"], reverse=True)

The result is an array, which was used in the previous article, using a xlsxwriter three-party library to manipulate Excel tables.

# 创建excel表格file = xlsxwriter.Workbook("dota.xlsx")# 创建工作表1sheet1 = file.add_worksheet("sheet1")# 创建表头headers = ["图片", "英雄", "ban", "", "图片", "英雄", "pick", "", "图片", "英雄", "bp_all"]for i, header in enumerate(headers):    # 第一行为表头    sheet1.write(0, i, header)

The first row is the table header, and the corresponding column fills the data

def insert_data(sheet1, headers, bp_list, col1, col2, col3):    for row in range(len(bp_list)):  # 行        # 设置行高        sheet1.set_row(row + 1, 30)        for col in range(len(headers)):  # 列            if col == col1:  # 英雄图片,根据id获取                url = "http://cdn.dotamore.com/heros_id_62_35/%d.png" % bp_list[row][1]["id"]                image_data = BytesIO(urlopen(url).read())                sheet1.insert_image(row + 1, col, url, {"image_data": image_data})            if col == col2:  # 英雄名                name = bp_list[row][1]["name_cn"]                sheet1.write(row + 1, col, name)            if col == col3:  # 统计次数                count = bp_list[row][1]["count"]                sheet1.write(row + 1, col, count)

Excel table generation, you can also insert a histogram.

def insert_chart(file, sheet1, bp_list, name, M, col_x, col_y):    chart = file.add_chart({"type": "column"})  # 柱状图    chart.add_series({        "categories": ["sheet1", 1, col_x, len(bp_list), col_x],  # 图表类别标签范围,x轴,这里取英雄的名字,即英雄名字那一列,行数根据数据列表确定        "values": ["sheet1", 1, col_y, len(bp_list), col_y],  # 图表数据范围,y轴,即次数那一列,行数根据数据列表确定        "data_labels": {"value": True},    })    chart.set_title({"name": name})  # 图表标题    chart.set_size({"width": 2000, "height": 400})    chart.set_x_axis({‘name‘: ‘英雄‘})  # x轴描述    chart.set_y_axis({‘name‘: ‘次数‘})  # y轴描述    chart.set_style(3)  # 直方图类型    sheet1.insert_chart(M, chart)  # 在表格M处插入柱状图

This is just a simple screening of the data, which can still be used to do more data analysis, data source: Knife Demon Data.
GitHub Address: Https://github.com/taixiang/py_dota

Finally put on the LGD pictures, hope they will be able to ti9 on the next year to do back

Welcome to follow my blog: https://blog.manjiexiang.cn/
More welcome attention number: Spring Breeze ten miles Better know you

There is a "Buddha system code Farming Circle", welcome everyone to join the chat, Happy good!


Expired, can add me tx467220125 pull you into the group.

Python tells you ti8 dota2 hero bp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.