import os, time, requests, json, re, sys
from retrying import retry
from urllib import parse """
文章描述:爬取王者荣耀英雄壁纸+封面
使用说明:直接在最底下输入下载地址,然后运行
作者:Felix(2020/7/30 14:42)
最新修改时间:2021-4-5
公众号:【全面资源集】
博客:https://blog.csdn.net/weixin_49012647
说明:没有使用进程,面向对象加过程,使用控制台输出显示进度,没有反扒机制,不识别UA,此文章调试了两天才趋近完美
""" class HonorOfKings:
"""王者荣耀皮肤下载"""
def __init__(self, save_path='./heros'):
self.save_path = save_path # 默认路径为:./heros
self.time = str(time.time()).split('.')
self.url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=%s' % \
self.time[0] # 这是抓包获得的,暂时不会。。 def hello(self):
"""这是排面"""
print("*" * 50)
print(' ' * 18 + '王者荣耀壁纸下载')
print(' ' * 5 + '公众号:【全面资源集】')
print("*" * 50)
return self def run(self):
"""爬虫主程序"""
print('↓' * 20 + ' 格式选择: ' + '↓' * 20)
print('1.缩略图 2.1024x768 3.1280x720 4.1280x1024 5.1440x900 6.1920x1080 7.1920x1200 8.1920x1440')
size = input('请输入您想下载的格式序号,默认6:')
print()
size = size if size and int(size) in [1, 2, 3, 4, 5, 6, 7, 8] else 6 # 直接回车就选6 hero_list = self.request('http://gamehelper.gm825.com/wzry/hero/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8').json()
hero_names = [] # 上面网址是抓包获得的,暂时不会。。
cover_div = os.path.join(self.save_path, '英雄封面')
os.makedirs(cover_div)
num = 0 # 下载第几张封面,用于显示进度
all = len(hero_list['list'])
for i in hero_list['list']:
hero_names.append(i['name']) # 把英雄名放入列表
content = self.request(i['cover']).content
cover_path = os.path.join(cover_div, i['name']+'.png')
if not os.path.exists(cover_path):
with open(cover_path, 'wb') as f: # 保存封面
f.write(content)
num += 1
sys.stdout.write('\r')
sys.stdout.write('→ → → →正在爬取封面....爬取进度:%s|%s张' % (num, all))
# print(hero_names) page = 0 # 第零页,用于获取英雄总数,并保存第零页图片
offset = 20 # 页数,用于递增爬取不同页
total_response = self.request(self.url.format(page)).text
total_res = json.loads(total_response)
total_page = --int(total_res['iTotalPages']) # 总页数(25)
print('→ → → →开始爬取皮肤...(总共 {} 页)'.format(total_page))
while True:
if offset > total_page:
break
url = self.url.format(offset)
result = self.request(url).json() # 获取json格式数据(不标准),但是能索引,你也可以用下面的
# response = self.request(url).text
# result = json.loads(response)
now = 0 # 表示第几张图,用于显示进度
for item in result["List"]:
now += 1
split_name = parse.unquote(item['sProdName']).split('-')
hero_name = split_name[0] # 英雄名,但是不规范
hero_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', hero_name) # 把垃圾符号弄掉
for f in hero_names: # 有些英雄名是:张良·幽兰居士,但是我希望所有同英雄皮肤放在一个目录下,所有加上这一步
if f in hero_name:
hero_name = f
# print('---正在下载第 {} 页 {} 英雄 进度{}/{}...'.format(offset, hero_name, now, len(result["List"])))
hero_url = parse.unquote(item['sProdImgNo_{}'.format(str(size))]) # 网址都被编码了,恶心
save_path = os.path.join(self.save_path, hero_name) # 图片保存路径
try: # 不是每个名字都有“-”
pic_name = split_name[1]
pic_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', pic_name)+'.jpg' # 图片名也给它标准化
except IndexError:
pic_name = hero_name+'.jpg'
save_name = os.path.join(save_path, pic_name)
if not os.path.exists(save_path):
os.makedirs(save_path)
if not os.path.exists(save_name):
with open(save_name, 'wb') as f:
response_content = self.request(hero_url.replace("/200", "/0")).content
f.write(response_content)
sys.stdout.write('\r') # 让输出不断更新
sys.stdout.write('第%s页 %s|第%s张' % (offset, '▋'*2*now, now))
offset += 1
print('\n下载完成!') @retry(stop_max_attempt_number=3)
def request(self, url):
response = requests.get(url, timeout=10)
assert response.status_code == 200
return response if __name__ == "__main__":
HonorOfKings(r'E:\win10\Pictures\电脑图片\王者荣耀壁纸').hello().run() # 这里设置图片下载根目录

加线程代码

import os, time, requests, json, re, sys

import threadpool
from retrying import retry
from urllib import parse
from tqdm import tqdm """
文章描述:爬取王者荣耀英雄壁纸+封面
使用说明:直接在最底下输入下载地址,然后运行
作者:Felix(2020/7/30 14:42)
最新修改时间:2021-4-4
公众号:【全面资源集】
博客:https://blog.csdn.net/weixin_49012647
说明:(1)使用线程爬取,但是感觉没有快多少,网址图片加载速度不是很快,而且服务器会没有响应。
(2)使用tqdm显示进度,但是该模块也会出问题,比如单位,img/s,结果变成s/img,而且加重程序负担
(3)因为是二次更改,在函数里嵌套函数,非常不专业,所有尽量少用
""" class HonorOfKings:
"""
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
""" def __init__(self, save_path='./heros'):
self.save_path = save_path # 保存根目录默认在代码所在目录
self.time = str(time.time()).split('.')
self.url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=%s' % \
self.time[0] # 抓包的网址 def hello(self):
"""
This is a welcome speech(欢迎界面) :return: self
"""
print("*" * 50)
print(' ' * 18 + '王者荣耀壁纸下载')
print(' ' * 5 + '公众号:【全面资源集】')
print("*" * 50)
return self def pool(self, function, arg):
"""下载线程池"""
pool = threadpool.ThreadPool(20)
request = threadpool.makeRequests(function, arg)
[pool.putRequest(req) for req in request]
pool.wait() def run(self):
"""The program entry(程序入口)"""
print('↓' * 20 + ' 格式选择: ' + '↓' * 20)
print('1.缩略图 2.1024x768 3.1280x720 4.1280x1024 5.1440x900 6.1920x1080 7.1920x1200 8.1920x1440')
size = input('请输入您想下载的格式序号,默认6:')
print()
size = size if size and int(size) in [1, 2, 3, 4, 5, 6, 7, 8] else 6 hero_list = self.request( # 下面网址是抓包获得的,暂时不会。。
'http://gamehelper.gm825.com/wzry/hero/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8').json()
cover_dicts = [] # 存放字典 {封面名:封面网址}
hero_names = [] # 存放所有英雄名
cover_div = os.path.join(self.save_path, '英雄封面')
os.makedirs(cover_div)
num = 0 # 下载第几张封面,用于显示进度
all = len(hero_list['list'])
def down_corver(dict):
"""下载封面"""
global num
content = self.request(dict['cover']).content
cover_path = os.path.join(cover_div, dict['name'] + '.png')
if not os.path.exists(cover_path):
with open(cover_path, 'wb') as f: # 保存封面
f.write(content)
num += 1
sys.stdout.write('\r')
sys.stdout.write('→ → → →正在爬取封面....爬取进度:%s|%s张' % (num, all))
for i in hero_list['list']:
cover_dicts.append({i['name']: i['corver']})
hero_names.append(i['name'])
# print(cover_dicts)
for i in hero_names:
os.makedirs(os.path.join(self.save_path, i))
self.pool(down_corver, cover_dicts) page = 0
offset = 0 # 爬取的页数
total_res = self.request(self.url.format(page)).json()
# total_response = self.request(self.url.format(page)).text
# total_res = json.loads(total_response)
total_page = --int(total_res['iTotalPages']) # 所有页数
print('→ → → →开始爬取皮肤(总共 {} 页)...'.format(total_page)) def down(dict):
"""创建线程池下载"""
if '-' in dict['name']:
hero_name = dict['name'].split('-')[0] # 英雄名,创建英雄图片目录
hero_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', hero_name)
for name in hero_names:
if name in hero_name:
hero_name = name
save_path = os.path.join(self.save_path, hero_name) # 英雄皮肤保存目录
pic_name = dict['name'].split('-')[1] # 各种皮肤名
pic_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', pic_name) + '.jpg'
else:
hero_name = pic_name = dict['name']
hero_name = pic_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', hero_name)
save_path = os.path.join(self.save_path, hero_name)
save_name = os.path.join(save_path, pic_name)
hero_url = dict['url']
if not os.path.exists(save_name):
with open(save_name, 'wb') as f:
response_content = self.request(hero_url.replace("/200", "/0")).content
f.write(response_content)
tq.update(1)
time.sleep(0.4) while True:
if offset > total_page:
break
url = self.url.format(offset)
response = self.request(url).text
result = json.loads(response) # 共25页,每页20个图片,总共483张;每页英雄不同,即乱排的
# now = 0
dict_list = [] # 储存所有{英雄名:下载地址}的列表
with tqdm(total=len(result["List"]), leave=False, unit='img', ncols=100) as tq:
tq.set_description('第%s页' % offset)
for item in result["List"]:
# now += 1
hero_name = parse.unquote(item['sProdName'])
# print('---正在下载第 {} 页 {} 英雄 进度{}/{}...'.format(offset, hero_name, now, len(result["List"])))
hero_url = parse.unquote(item['sProdImgNo_{}'.format(str(size))])
dict_list.append({'name': hero_name, 'url': hero_url}) # 把所有对应英雄名及图片下载地址放进列表
self.pool(down, dict_list)
offset += 1
print('下载完成!') @retry(stop_max_attempt_number=3)
def request(self, url):
"""
Send a request :param url: the url of request
:param timeout: the time of request
:return: the result of request
"""
response = requests.get(url, timeout=10)
assert response.status_code == 200
return response if __name__ == "__main__":
HonorOfKings(save_path=r'E:\win10\Pictures\电脑图片\王者荣耀壁纸').hello().run()

更多资源请关注:【全面资源集

python 爬取王者荣耀英雄皮肤代码的更多相关文章

  1. Python爬取 | 王者荣耀英雄皮肤海报

    这里只展示代码,具体介绍请点击下方链接. Python爬取 | 王者荣耀英雄皮肤海报 import requests import re import os import time import wi ...

  2. 利用python爬取王者荣耀英雄皮肤图片

    前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...

  3. python学习--第二天 爬取王者荣耀英雄皮肤

    今天目的是爬取所有英雄皮肤 在爬取所有之前,先完成一张皮肤的爬取 打开anacond调出编译器Jupyter Notebook 打开王者荣耀官网 下拉找到位于网页右边的英雄/皮肤 点击[+更多] 进入 ...

  4. Python 爬取 "王者荣耀.英雄壁纸" 过程中的矛和盾

    1. 前言 学习爬虫,最好的方式就是自己编写爬虫程序. 爬取目标网站上的数据,理论上讲是简单的,无非就是分析页面中的资源链接.然后下载.最后保存. 但是在实施过程却会遇到一些阻碍. 很多网站为了阻止爬 ...

  5. 用Python爬取"王者农药"英雄皮肤

    0.引言 作为一款现象级游戏,王者荣耀,想必大家都玩过或听过,游戏里中各式各样的英雄,每款皮肤都非常精美,用做电脑壁纸再合适不过了.本篇就来教大家如何使用Python来爬取这些精美的英雄皮肤. 1.环 ...

  6. 用Python爬取"王者农药"英雄皮肤 原

    padding: 10px; border-bottom: 1px solid #d3d3d3; background-color: #2e8b57; } .second-menu-item { pa ...

  7. python爬取王者荣耀全英雄皮肤

    import os import requests url = 'https://pvp.qq.com/web201605/js/herolist.json' herolist = requests. ...

  8. python爬虫---爬取王者荣耀全部皮肤图片

    代码: import requests json_headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win ...

  9. python 爬取王者荣耀高清壁纸

    代码地址如下:http://www.demodashi.com/demo/13104.html 一.前言 打过王者的童鞋一般都会喜欢里边设计出来的英雄吧,特别想把王者荣耀的英雄的高清图片当成电脑桌面 ...

随机推荐

  1. CMD(命令提示符)的基本操作(文件夹)

    打开CMD窗口,接下来将介绍如何使用CMD来创建.删除.修改.查看文件夹(目录) ps:以下所有文件夹将统一写成目录 1.1 使用CMD创建空目录(为了更好的演示,本文皆以D盘为当前路径),命令如下: ...

  2. CTS camera的基础操作和debug

    手机端 设置永久不锁屏 1 CTS 进入cts目录tools 运行以下命令 ./cts-tradefed adb devices找设备数串 整跑 run cts -m CtsCameraTestCas ...

  3. Flask:处理Web表单

    尽管 Flask 的请求对象提供的信息足以处理 Web 表单,但有些任务很单调,而且要重复操作.比如,生成表单的 HTML 代码和验证提交的表单数据.Flask-WTF 扩展可以把处理 Web 表单的 ...

  4. 9.Vue之webpack打包基础---模块化思维

    主要内容: 1. 什么是模块化思维? 2.  ES6包的封装思想 一.什么是模块化思维呢? 现实工作中, 一个项目可能会有多个人同时开发. 然后, 将所有人开发的内容, 合并到一个文件中. 比如: 1 ...

  5. Serverless 2.0,鸡蛋还是银弹?

    简介: 本篇旨在介绍 Serverless 如今应用到应用(非病句)的各种困境,以及帮助用户如何去规避一些问题,提前了解方向. 浪潮 从 2014 年 Serverless 冒头至今,已经有无数的勇士 ...

  6. python学习之常用数据结构

    前言:数据结构不管在哪门编程语言之中都是非常重要的,因为学校的课程学习到了python,所以今天来聊聊关于python的数据结构使用. 一.列表 list 1.列表基本介绍 列表中的每个元素都可变的, ...

  7. 第01章-Java SE8的流库

    从迭代到流的操作 流表面上看起来和集合很类似,都可以让我们转换和获取数据,但是它们之间存在着显著的差异 流并不存储其元素,这些元素可能存储在底层的集合中,或者是按需生成的 流的操作不会修改其数据源 流 ...

  8. Java 面向对象 03

    面向对象·三级 代码块的概述和分类 * A:代码块概述     * 在Java中,使用 { } 括起来的代码被称为代码块. * B:代码块分类     * 根据其位置和声明的不同,可以分为局部代码块, ...

  9. Reactive Spring实战 -- WebFlux使用教程

    WebFlux是Spring 5提供的响应式Web应用框架. 它是完全非阻塞的,可以在Netty,Undertow和Servlet 3.1+等非阻塞服务器上运行. 本文主要介绍WebFlux的使用. ...

  10. kali msf6 更新及bug处理

    问题描述 Metasploit 漏洞库更新,利用msfupdate命令更新,出现已停止该命令更新,出现如下提示: 利用一句话安装更新,命令如下,安装过程中有部分警告出现 curl https://ra ...