利用python爬虫爬取LOL英雄联盟英雄数据和皮肤原画
思路步骤:
- 准备一个CSV放英雄的皮肤名称,包括炫彩皮肤名称
- 准备一个CSV放英雄的皮肤名称,包括被动技能和QWER技能
- 准备在工作目录下按英雄生成文件夹并下载保存该英雄所有皮肤原画
- 爬取官网数据,获取js文件,包含英雄名称和编号
- 循环读取,英雄技能按顺序放入CSV表格
- 下载原画保存到各个文件夹
代码如下 :
#!/usr/bin/env python
# -- coding = 'utf-8' --
# @Python : 3.7
# @OS : Windows 10 kiton.
# @Time : 2021/6/6 18:44
# @Author :
# @E-mail : 1154282938@qq.com
# @File : 英雄皮肤.py
# @Software: PyCharm
import csv
import requests
import os
import jsonpath
from urllib.request import urlretrieve
#获取ID
def get_id():
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
headers = {
'user - agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 81.0.4044.138Safari / 537.36'
}
response = requests.get(url=url, headers=headers)
r = response.json()
ids = jsonpath.jsonpath(r, '$..heroId')
print(ids)
print("英雄的个数为: " + str(len(ids)))
return ids
#获取皮肤
def get_skins(ids):
headers = {
'user - agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 81.0.4044.138Safari / 537.36'
}
for heroId in ids:
url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(heroId)
response = requests.get(url=url, headers=headers).json()
skins = response['skins']
Img = jsonpath.jsonpath(skins, '$..mainImg')
names = jsonpath.jsonpath(response, '$..name')
print(names)
# E 0 Q R W
jineng = [names[0], ]
jineng.append(names[-4])
jineng.append(names[-3])
jineng.append(names[-1])
jineng.append(names[-5])
jineng.append(names[-2])
# jineng = names[-5:]
# jineng.insert(0, names[0])
names.pop(-5)
names.pop(-4)
names.pop(-3)
names.pop(-2)
names.pop(-1)
with open("./lolpifu.csv", 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(names)
with open("./loljineng.csv", 'a', newline='', encoding='utf-8') as file2:
writer = csv.writer(file2, delimiter=',')
writer.writerow(jineng)
try:
if not os.path.exists(names[0]):
os.mkdir(names[0])
for name, Imgs in zip(names, Img):
urlretrieve(Imgs,names[0] + '/' + name + '.jpg')
except:
pass
print('<%s>' % names)
if ids == 3:
break
id_list = get_id()
get_skins(id_list)
print(id_list)
结果如下:
D:\ruanjian\anaconda202002\python.exe F:/vscode-python-kiton/get_lol/英雄皮肤.py
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '48', '50', '51', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '67', '68', '69', '72', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '89', '90', '91', '92', '96', '98', '99', '101', '102', '103', '104', '105', '106', '107', '110', '111', '112', '113', '114', '115', '117', '119', '120', '121', '122', '126', '127', '131', '133', '134', '136', '141', '142', '143', '145', '147', '150', '154', '157', '161', '163', '164', '201', '202', '203', '222', '223', '234', '235', '236', '238', '240', '245', '246', '254', '266', '267', '268', '350', '360', '412', '420', '421', '427', '429', '432', '497', '498', '516', '517', '518', '523', '526', '555', '777', '875', '876', '887']
英雄的个数为: 155
['黑暗之女', '黑暗之女', '哥特萝莉 安妮', '小红帽 安妮', '安妮梦游仙境', '舞会公主 安妮', '冰霜烈焰 安妮', '安伯斯与提妮', '科学怪熊的新娘 安妮', '“你看见过我的熊猫吗?”安妮', '甜心宝贝 安妮', '海克斯科技 安妮', '银河魔装机神 安妮', '十周年纪念 安妮', '福牛守护者 安妮', '福牛守护者 安妮 贺岁', '福牛守护者 安妮 如意', '福牛守护者 安妮 迎春', '福牛守护者 安妮 进宝', '福牛守护者 安妮 纳福', '福牛守护者 安妮 吉祥', '福牛守护者 安妮 团圆', '熔岩护盾', '嗜火', '碎裂之火', '提伯斯之怒', '焚烧']
各英雄皮肤名称列表
各英雄技能名称列表
各英雄皮肤原画
网友评论