Python爬取云顶之弈S4赛季官方攻略

  • 2021年4月28日
  • 技术

云顶数据还是比较好处理的,都是接口形式,更多是进行数据的整理。

分别封装了获取英雄数据,装备数据,职业数据,羁绊数据,阵容列表,阵容攻略数据的方法,可以分开独立调用,后续随着官方更新数据有所改动。

使用的Python版本为3.7。

# coding=utf-8
import json
import requests


class TFT():  # 云顶攻略类
    def __init__(self):
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'}

    def strUnescape(self, str):  # 替换转义字符
        str = str.replace("'", "\'")
        str = str.replace("</p>", "}")
        str = str.replace("<p>", "")
        str = str.replace("<\/p>", "")
        str = str.replace("\n", "");
        str = str.replace("\r", "");
        str = str.replace("\r\n", "");
        return str

    def chessList(self):  # 获取英雄数据
        res = requests.get("http://game.gtimg.cn/images/lol/act/img/tft/js/chess.js", headers=self.headers)
        d = json.loads(res.text)
        chess = []
        for i in d['data']:
            chess.append(i)
        return chess

    def equipList(self):  # 获取装备数据
        res = requests.get("http://game.gtimg.cn/images/lol/act/img/tft/js/equip.js", headers=self.headers)
        d = json.loads(res.text)
        equip = []
        delEquips = ['201', '202', '203', '205', '206', '207', '208', '209', '210', '211', '212', '317', '324', '331', '333', '337', '340', '341', '342', '346', '349', '352', '355', '403']
        # 删除版本去除装备,equipId
        for i in d['data']:
            # 删除版本去除的装备
            if i['equipId'] not in delEquips:
                equip.append(i)
        return equip

    # 获取职业数据
    def jobList(self):
        res = requests.get('http://game.gtimg.cn/images/lol/act/img/tft/js/job.js', headers=self.headers)
        d = json.loads(res.text)
        job = []
        for i in d['data']:
            job.append(i)
        return job

    def raceList(self):  # 获取所有的羁绊种族 返回一个列表
        res = requests.get('http://game.gtimg.cn/images/lol/act/img/tft/js/race.js', headers=self.headers)
        d = json.loads(res.text)
        race = []
        for i in d['data']:
            race.append(i)
        return race

    def lineList(self):  # 获取阵容列表
        res = requests.get("http://game.gtimg.cn/images/lol/act/tftzlkauto/json/lineupJson/s4/6/lineup_detail_total.json", headers=self.headers)
        d = json.loads(res.text)
        linelist = []
        for i in d['lineup_list']:
            if i["quality"] == '':
                continue
            detailArr = json.loads(self.strUnescape(i["detail"]))
            i['line_name'] = detailArr['line_name']
            i['strategy'] = detailArr
            # 排除删除的卡组
            if i['sortID'] != '':
                linelist.append(i)

        return linelist

    def strategy(self, id):  # 阵容详情
        res = requests.get("http://game.gtimg.cn/images/lol/act/tftzlkauto/json/lineupJson/s4/6/" + id + ".json", headers=self.headers)
        text = TFT.strUnescape(self, res.text)
        text = text.replace("\'", "\"")
        d = json.loads(text)
        detail = json.loads(TFT.strUnescape(self, d['detail']))

        lineup_name = detail['line_name']  # 卡组名
        # 英雄站位
        hero_location = detail['hero_location']
        # 前期
        if 'early_heros' in str(detail):
            early_heros = detail['early_heros']
        else:
            early_heros = ''
        # 中期
        if 'metaphase_heros' in str(detail):
            metaphase_heros = detail['metaphase_heros']
        else:
            metaphase_heros = ''
        # 追3星英雄
        if 'level_3_heros' in str(detail):
            level_3_heros = detail['level_3_heros']
        else:
            level_3_heros = ""

        # 天选英雄
        if 'early_chosen_heros' in str(detail):
            early_chosen_heros = detail['early_chosen_heros']
        else:
            early_chosen_heros = ''
        # 天选备选英雄
        if 'replace_chosen_heros' in str(detail):
            replace_chosen_heros = detail['replace_chosen_heros']
        else:
            replace_chosen_heros = ''

        # 备选英雄
        if 'hero_replace' in str(detail):
            hero_replace = detail['hero_replace']
        else:
            hero_replace = ""

        # 攻略
        # 早期过渡
        early_info = detail['early_info']
        # 搜牌节奏
        d_time = detail['d_time']
        # 装备分析
        if 'equipment_info' in str(detail):
            equipment_info = detail['equipment_info']
        else:
            equipment_info = ''
        # 阵容站位
        if 'location_info' in str(detail):
            location_info = detail['location_info']
        else:
            location_info = ''
        # 克制分析
        if 'enemy_info' in str(detail):
            enemy_info = detail['enemy_info']
        else:
            enemy_info = ''

        strategy = {'lineup_name': lineup_name,
                    'hero_location': hero_location,
                    'early_heros': early_heros,
                    "metaphase_heros": metaphase_heros,
                    'level_3_heros': level_3_heros,
                    'hero_replace': hero_replace,
                    'early_chosen_heros': early_chosen_heros,
                    'replace_chosen_heros': replace_chosen_heros,
                    'early_info': early_info,
                    'd_time': d_time,
                    'equipment_info': equipment_info,
                    'location_info': location_info,
                    'enemy_info': enemy_info,
                    "rel_time": d['rel_time']}
        return strategy


def main():
    tft = TFT()
    # 获取英雄数据
    chess = tft.chessList()
    # 获取装备数据
    equip = tft.equipList()
    # 获取职业数据
    job = tft.jobList()
    # 获取羁绊数据
    race = tft.raceList()
    # 获取阵容列表
    line = tft.lineList()

    for i in line:
        # 获取阵容攻略数据
        strategy = tft.strategy(i['id'])
        print(strategy)


if __name__ == "__main__":
    main()

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注