python爬虫实践之爬取王者荣耀皮肤

详细代码如下,具体的就不想分析了,代码中有注释。

import re
import time
import urllib.parse

import requests

# 爬虫实战:爬取王者荣耀皮肤

# 第1页的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=0&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735
# 第2页的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=1&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735
# 第3页的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=2&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735
# ...
# 第20页的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=19&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735

# 图片尺寸大小
# 指定尺寸:1024x768对应: "sProdImgNo_2": "http%3A%2F%2Fshp%2Eqpic%2Ecn%2Fishow%2F2735120509%2F1575509923%5F84828260%5F24055%5FsProdImgNo%5F2%2Ejpg%2F200",
# size2List = re.findall('"sProdImgNo_2":"(.*?)"', response)
# 指定尺寸:1280x720对应sProdImgNo_3
# size3List = re.findall('"sProdImgNo_3":"(.*?)"', response)
# 指定尺寸:1280x1024对应sProdImgNo_4
# size4List = re.findall('"sProdImgNo_4":"(.*?)"', response)
# 指定尺寸:1440x900对应sProdImgNo_5
# size5List = re.findall('"sProdImgNo_5":"(.*?)"', response)
# 指定尺寸:1920x1080对应sProdImgNo_6
# size6List = re.findall('"sProdImgNo_6":"(.*?)"', response)
# 指定尺寸:1920x1200对应sProdImgNo_7
# size7List = re.findall('"sProdImgNo_7":"(.*?)"', response)
# 指定尺寸:1920x1440对应sProdImgNo_8
# size8List = re.findall('"sProdImgNo_8":"(.*?)"', response)

print("[2]1024×768\t\t[3]1280×720\t\t[4]1280×1024\t\t[5]1440×900\n[6]1920×1080\t\t[7]1920×1200\t\t[8]1920×1440")
size = input("请在以上尺寸中选择一项并输入括号内的数字(如选择1024×768输入2):")
pageTotal = 3  # 总页数
for pageIndex in range(0, pageTotal):

    # 组装URL
    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=" + str(
        pageIndex) + "&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735"

    # 选择图片尺寸大小
    imageSize = {"2": "sProdImgNo_2", "3": "sProdImgNo_3", "4": "sProdImgNo_4", "5": "sProdImgNo_5",
                 "6": "sProdImgNo_6", "7": "sProdImgNo_7", "8": "sProdImgNo_8"}

    # 发送请求,获取响应
    response = requests.get(url).content.decode("utf-8")

    # 图片名称
    # "sProdName": "%E8%94%A1%E6%96%87%E5%A7%AC%2D%E7%B9%81%E6%98%9F%E5%90%9F%E6%B8%B8",
    namesList = re.findall('"sProdName":"(.*?)"', response)

    # 图片URL
    sizeList = re.findall('"' + imageSize[size] + '":"(.*?)"', response)

    for i in range(0, len(namesList)):
        imageUrl = urllib.parse.unquote(sizeList[i]).replace("/200", "/0")
        data = requests.get(imageUrl).content
        print("正在下载:" + urllib.parse.unquote(namesList[i]) + ".jpg......")
        with open(r"C:/Users/Administrator/Pictures/王者荣耀皮肤/" + urllib.parse.unquote(namesList[i]) + ".jpg",
                  "wb") as file_object:
            file_object.write(data)
            time.sleep(0.5)

下载效果如下:

python爬虫实践之爬取王者荣耀皮肤_第1张图片

具体图片:

python爬虫实践之爬取王者荣耀皮肤_第2张图片

你可能感兴趣的:(#,python爬虫实践)