Matlab爬虫获取王者荣耀英雄皮肤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Matlab爬虫获取王者荣耀英雄皮肤相关的知识,希望对你有一定的参考价值。

前言:周末闲来无事,玩了几局王者荣耀,突发奇想怎么获取到王者荣耀里面的英雄皮肤,本期分享一下如何通过matlab爬虫批量提取王者荣耀的英雄皮肤

关键字:王者荣耀、爬虫、Matlab


首先在度娘上找到王者荣耀的英雄主页Matlab爬虫获取王者荣耀英雄皮肤_html

​https://pvp.qq.com/web201605/herolist.shtml​

Matlab爬虫获取王者荣耀英雄皮肤_html_02

通过chrome浏览器的F12,找到获取英雄的调用方法。

 Matlab爬虫获取王者荣耀英雄皮肤_html_03


双击这个herolist.json,通过Notepad++打开可以看到里面对应的英雄详情

 

Matlab爬虫获取王者荣耀英雄皮肤_创建文件夹_04

 

在matlab通过urlread函数获取到英雄列表情况,由于获取到的数据为char,需要转换为json格式,通过jsondecode函数实现转换 

url = https://pvp.qq.com/web201605/js/herolist.json;try    herolist = urlread(url);catch    disp(提取英雄列表失败,请再次重试)endjsonData=jsondecode(herolist);

对每个英雄提取对应的ename、cname和skin_name变量,

 

[row, col] = size(jsonData);hero_name = cell(row,1);hero_number = cell(row,1);hero_skin_number = cell(row,1);for i = 1:row    hero_namei = jsonDatai,1.cname;    hero_numberi = jsonDatai,1.ename;    try        skin_name = strsplit(jsonDatai,1.skin_name, |);        hero_skin_numberi = length(skin_name);    catch        hero_skin_numberi = 1;    endend

下面开始要提取对应的图片,例如任意选择一个英雄,网址为:

​https://pvp.qq.com/web201605/herodetail/508.shtml​

Matlab爬虫获取王者荣耀英雄皮肤_html_05

右击选择图片-->检查,

Matlab爬虫获取王者荣耀英雄皮肤_html_06

可以看到对应的图片路径:

​https://game.gtimg.cn/images/yxzj/img201606/heroimg/508/508-smallskin-2.jpg​

Matlab爬虫获取王者荣耀英雄皮肤_创建文件夹_07

根据上面的网址,可以得到对应的图片路径格式为:

​https://game.gtimg.cn/images/yxzj/img201606/heroimg/hero_number​

/hero_number-smallskin-hero_skin_number.jpg

 通过webread函数提取到对应的图片,然后获取的图片通过imwrite保存成jpg文件。

 

onehero_link = strcat(http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/ ...            , num2str(hero_numberi), /, num2str(hero_numberi), -bigskin-, num2str(k),.jpg);try    image = webread(onehero_link);catch     disp([获取英雄,hero_namei,图片失败,请再次重试]) end imwrite(image,strcat(file_path_name_, num2str(k), .jpg));

通过urlread和webread在网络不好的情况下,可能会出现报错,所以通过try catch避免程序异常报错。对于每个英雄都单独保存在对应的名称文件夹下,通过mkdir创建文件夹。

  

完整的代码:

url = https://pvp.qq.com/web201605/js/herolist.json;try    herolist = urlread(url);catch    disp(提取英雄列表失败,请再次重试)end
jsnotallow=jsondecode(herolist);[row, col] = size(jsonData);hero_name = cell(row,1);hero_number = cell(row,1);hero_skin_number = cell(row,1);for i = 1:row hero_namei = jsonDatai,1.cname; hero_numberi = jsonDatai,1.ename; try skin_name = strsplit(jsonDatai,1.skin_name, |); hero_skin_numberi = length(skin_name); catch hero_skin_numberi = 1; endendsavepath = C:\\Users\\xxx\\Documents\\MATLAB;for i = 1:row file_name = hero_namei; file_path_name = strcat(savepath,\\,file_name); file_path_name_ = strcat(file_path_name,\\); mkdir(file_path_name_); for k = 1:hero_skin_numberi onehero_link = strcat(http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/ ... , num2str(hero_numberi), /, num2str(hero_numberi), -bigskin-, num2str(k),.jpg); try image = webread(onehero_link); catch disp([获取英雄,hero_namei,图片失败,请再次重试]) end imwrite(image,strcat(file_path_name_, num2str(k), .jpg)); endend

 

最后提取的结果如下

Matlab爬虫获取王者荣耀英雄皮肤_创建文件夹_08

Matlab爬虫获取王者荣耀英雄皮肤_json_09

最喜欢的英雄程咬金结束本文

Matlab爬虫获取王者荣耀英雄皮肤_json_10

附上Python版本(推荐):

import osimport requests
url = https://pvp.qq.com/web201605/js/herolist.jsonherolist = requests.get(url) # 获取英雄列表json文件
herolist_json = herolist.json() # 转化为json格式hero_name = list(map(lambda x: x[cname], herolist.json())) # 提取英雄的名字hero_number = list(map(lambda x: x[ename], herolist.json())) # 提取英雄的编号hero_skin_number = []for i in herolist.json(): try: hero_skin_number.append(len(i[skin_name].split("|"))) except KeyError: hero_skin_number.append(1)

# 下载图片def downloadPic(): i = 0 for j in hero_number: # 创建文件夹 os.mkdir("./" + hero_name[i]) # 进入创建好的文件夹 os.chdir("./" + hero_name[i]) i += 1 for k in range(1, hero_skin_number[i - 1] + 1): # 拼接url onehero_link = http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/ + str(j) + / + str( j) + -bigskin- + str(k) + .jpg print(onehero_link) im = requests.get(onehero_link) # 请求url if im.status_code == 200: open(str(k) + .jpg, wb).write(im.content) # 写入文件 os.chdir("../")
downloadPic()


以上是关于Matlab爬虫获取王者荣耀英雄皮肤的主要内容,如果未能解决你的问题,请参考以下文章

Python 爬虫 采集王者荣耀英雄皮肤

王者荣耀五周年,爬取102个英雄+326款皮肤,分析上线时间

python爬虫-20行代码爬取王者荣耀所有英雄图片,小白也轻轻松松

python爬虫-20行代码爬取王者荣耀所有英雄图片,小白也轻轻松松

爬虫小程序 - 王者荣耀全皮肤爬取

教你用python爬取王者荣耀英雄皮肤图片,并将图片保存在各自英雄的文件夹中。(附源码)