Python通过requests模块实现抓取王者荣耀全套皮肤

作者:小雁子学Python 时间:2022-04-06 05:47:12 

前言

今天带大家爬取王者荣耀全套皮肤,废话不多说,直接开始~

开发工具

Python版本: 3.6.4

相关模块:

requests模块;

urllib模块;

以及一些Python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

思路分析

1、打开官方王者荣耀壁纸网站
网站地址:https://pvp.qq.com/web201605/wallpaper.shtml

2、快捷键F12,调出控制台进行抓包

Python通过requests模块实现抓取王者荣耀全套皮肤

3、找到正确的链接并分析

Python通过requests模块实现抓取王者荣耀全套皮肤

4、查看返回数据格式

Python通过requests模块实现抓取王者荣耀全套皮肤

Python通过requests模块实现抓取王者荣耀全套皮肤

5、解析url链接

Python通过requests模块实现抓取王者荣耀全套皮肤

6、查看url内容是否是所需图片,发现其实是缩略图

Python通过requests模块实现抓取王者荣耀全套皮肤

7、那就去分析网站,随便点开一张壁纸,查看指定格式的链接

Python通过requests模块实现抓取王者荣耀全套皮肤

8、找到目标地址

Python通过requests模块实现抓取王者荣耀全套皮肤

9、分析目标链接和缩略图的链接区别
缩略图:http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/200

目标图:http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/0

可以知道,将指定格式的缩略图地址后面200替换成0就是目标真实图片

代码实现


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通过requests模块实现抓取王者荣耀全套皮肤

Python通过requests模块实现抓取王者荣耀全套皮肤

来源:https://blog.csdn.net/weixin_43649691/article/details/120992429

标签:Python,抓取王者荣耀皮肤,requests
0
投稿

猜你喜欢

  • python PyTorch参数初始化和Finetune

    2023-04-26 08:53:44
  • 提升Python运行速度的5个小技巧

    2021-07-28 20:25:24
  • 详解Python中sorted()和sort()的使用与区别

    2022-05-06 17:39:09
  • numpy中np.dstack()、np.hstack()、np.vstack()用法

    2021-08-27 11:47:42
  • python如何进行矩阵运算

    2021-05-01 22:58:25
  • django执行原生SQL查询的实现

    2023-11-11 13:34:04
  • SQL Server 安全检查列表全攻略

    2008-01-29 13:31:00
  • Python实现配置文件备份的方法

    2021-06-11 01:39:03
  • javascript基础之indexOf函数

    2008-06-25 13:28:00
  • python 获得任意路径下的文件及其根目录的方法

    2022-02-02 17:32:15
  • 使用Python和GDAL给图片加坐标系的实现思路(坐标投影转换)

    2021-11-10 03:15:31
  • python np.arange 步长0.1的问题需要特别注意

    2021-09-26 08:49:49
  • Python基于爬虫实现全网搜索并下载音乐

    2023-12-26 16:36:40
  • python实现使用遗传算法进行图片拟合

    2022-05-25 03:31:53
  • Python标准库json模块和pickle模块使用详解

    2021-12-24 16:17:14
  • python实现人脸识别代码

    2022-12-20 15:39:06
  • python3.6+opencv3.4实现鼠标交互查看图片像素

    2021-09-09 05:27:12
  • 对numpy Array [: ,] 的取值方法详解

    2023-02-05 17:33:23
  • 一个滑动展示的小代码

    2011-07-01 12:58:05
  • Python疫情数据可视化分析

    2022-05-22 05:09:15
  • asp之家 网络编程 m.aspxhome.com