目录开发工具环境搭建思路分析代码实现前言今天带大家爬取王者荣耀全套皮肤,废话不多说,直接开始~开发工具Python版本:3.6.4相关模块:requests模块;urllib模块;以及一些Pytho…
目录
-
开发工具
-
环境搭建
-
思路分析
-
代码实现
前言
今天带大家爬取王者荣耀全套皮肤,废话不多说,直接开始~
开发工具
python版本:3.6.4
相关模块:
requests模块;
urllib模块;
以及一些Python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
思路分析
1、打开官方王者荣耀壁纸网站
网站地址:https://pvp.qq.com/web201605/wallpaper.shtml
2、快捷键F12,调出控制台进行抓包
3、找到正确的链接并分析
4、查看返回数据格式
5、解析url链接
6、查看url内容是否是所需图片,发现其实是缩略图
7、那就去分析网站,随便点开一张壁纸,查看指定格式的链接
8、找到目标地址
9、分析目标链接和缩略图的链接区别
代码实现
import os, time, requests, json, re
from retrying import retry
from urllib import parse
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 + '作者: Felix Date: 2020-05-20 13:14')
print("*" * 50)
return self
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:')
size = size if size and int(size) in [1,2,3,4,5,6,7,8] else 6
print('---下载开始...')
page = 0
offset = 0
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))
while True:
if offset > total_page:
break
url = self.url.format(offset)
response = self.request(url).text
result = json.loads(response)
now = 0
for item in result["List"]:
now += 1
hero_name = parse.unquote(item['sProdName']).split('-')[0]
hero_name = re.sub(r'[【】:.<>|@#$%^&() ]', '', hero_name)
print('---正在下载第 {} 页 {} 英雄 进度{}/{}...' . format(offset, hero_name, now, len(result["List"])))
hero_url = parse.unquote(item['sProdImgNo_{}'.format(str(size))])
save_path = self.save_path + '/' + hero_name
save_name = save_path + '/' + hero_url.split('/')[-2]
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)
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().hello().run()
本期完整源代码文末获取
代码运行结果
关于Python技术储备
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。