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

时间:2021-08-22 00:38:01

这里只展示代码,具体介绍请点击下方链接。

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

import requests
import re
import os
import time
import winreg count = 0 # 计数用 # 获取第一次页面所以英雄的id以及名字
def first_url(url):
response = requests.get(url)
response.encoding = 'gbk'
response = response.text
id_name = re.findall('<li><a href=.*?/(\d{3}).*?alt=.*?>(.*?)</a></li>', response, re.S)
return id_name # 获取英雄皮肤昵称以及获取皮肤数目
def second_url(url, name):
response = requests.get(url)
response.encoding = 'gbk'
response = response.text
# 第66行
# <ul class="pic-pf-list pic-pf-list3" data-imgname="月光之女&0|哥特玫瑰&1|绯红之刃&0|紫霞仙子&12|一生所爱&44|瓷语鉴心&1">
response = re.findall('<ul class=.*?data-imgname="(.*?)">', response)
response = response[0].split('|')
skin_names = []
for i in response:
names = re.sub('[^\u4e00-\u9fa5]', '', i) + ' ' + name
skin_names.append(names)
return skin_names def download(url, skin_names, path):
num = len(skin_names) # 获取英雄皮肤数目,以控制循环次数
times = str(num) # 将皮肤个数字符化,用于文件名称显示皮肤个数
name = skin_names[0].split(' ')[-1] # 这里是获取英雄名字,如露娜,用于后面皮肤全称的拼接
path = path + '/' + name + ' ' + times + '张' # 皮肤存储路径
if not os.path.exists(path): # 如果路径不存在,则自动创建
os.mkdir(path)
for n in range(0, num): # 循环
skin_url = url + str(n + 1) + '.jpg' # 皮肤图片的链接
image_path = path + '/' + skin_names[n] + '.jpg' # 每张皮肤的命名
global count # 定义全局变量,用于统计皮肤下载张数
count = count + 1
print(f' 第{count}张 正在下载>>{name}<<的第{str(n + 1)}张皮肤,共{times}张')
with open(image_path, 'wb') as f: # 皮肤图片下载并保存
f.write(requests.get(skin_url).content)
time.sleep(0.3) def get_desktop():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') # 利用系统的链表
zm = winreg.QueryValueEx(key, "Desktop")[0] # 获取的是Unicode类型数据
return str(zm) # Unicode转化为str并返回 def main():
path = f'{get_desktop()}\\王者荣耀角色皮肤图片'
print(path)
if not os.path.exists(path):
os.mkdir(path)
url = 'https://pvp.qq.com/web201605/herolist.shtml'
id_name = first_url(url)
# 拼接链接
for i in id_name:
hero_url = f'http://pvp.qq.com/web201605/herodetail/{i[0]}.shtml'
skin_names = second_url(hero_url, i[1]) # i[1]是英雄的名字,比如露娜,云中君等
skin_url = f'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{i[0]}/{i[0]}-bigskin-'
download(skin_url, skin_names, path) if __name__ == '__main__':
main()