玩游戏怎么能没有皮肤,Python一键采集王某耀游戏所有皮肤,这波就很舒服

import requests  # 数据请求模块 第三方模块 pip install requests
import re  # 正则表达式模块 内置模块
import os # 文件操作模块
1. 发送请求
url = 'https://***.com/web201605/js/herolist.json'  # 确定请求url
加上headers请求头进行伪装
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}
通过requests这个模块里面get请求方法对于url地址发送请求, 并且携带上headers请求头伪装, 最后用response变量接收返回数据
response = requests.get(url=url, headers=headers)
print(response.json())  #
print(type(response.json()))
print(response.text)    #
print(type(response.text))
2. 获取数据, 获取服务器返回的response响应数据
3. 解析数据, 提取我们想要的数据内容英雄ID 以及 英雄名字
for index in response.json():
    # 字典取值 根据冒号左边的内容 提取冒号右边的内容 键值对取值
    hero_id = index['ename']    # 英雄ID
    hero_name = index['cname']  # 英雄名字
    # 相对路径 你代码在哪个文件夹里面, 生成的就是那个
    # 绝对路径 指定那个硬盘那个文件里面
    path = f'{hero_name}\\'
    if not os.path.exists(path):  # 判断是否有这个文件夹
        os.mkdir(path) # 没有创建

    # 字符串格式化方法
    hero_url = f'https://****.com/web201605/herodetail/{hero_id}.shtml'
    # 4.发送请求, 对于英雄的详情页发送请求
    response_1 = requests.get(url=hero_url, headers=headers)
    # 遇到乱码, 直接进行转码
    response_1.encoding = response_1.apparent_encoding # 自动识别编码
    # print(hero_id, hero_name, hero_url)
    # print(response_1.text)
    title_info = re.findall('', response_1.text)[0]
    title_info = re.sub('&\d+', '', title_info).split('|')
    # len(title_info) >>> 4  # 统计列表有多少个元素,
    print(title_info) # len(title_info) + 1 >>> 5
    for i in range(1, len(title_info) + 1):  # 顾头不顾尾
        img_url = f'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{i}.jpg'
        img_name = title_info[i-1]
        print(img_name, img_url)
        img_content = requests.get(url=img_url, headers=headers).content # 获取图片二进制数据
        with open(path + str(img_name) + '.jpg', mode='wb') as f:
            f.write(img_content)
        print(hero_name, img_name)

Original: https://www.cnblogs.com/hahaa/p/15927990.html
Author: 轻松学Python
Title: 玩游戏怎么能没有皮肤,Python一键采集王某耀游戏所有皮肤,这波就很舒服

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509950/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球