40行python代码搞定王者荣耀全部壁纸下载

大家好,我是才哥。

今天我们一起来采集王者荣耀英雄的全部皮肤地址,目标网址:

https://pvp.qq.com/web201605/herolist.shtml

09f0a77416da14e0b348eb876ccabffb.png

通过开发者工具发现 https://pvp.qq.com/web201605/js/herolist.json接口返回了全部英雄数据。

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
}
herolist = requests.get(
    "https://pvp.qq.com/web201605/js/herolist.json", headers=headers).json()
herolist
[{'ename': 105,
  'cname': '廉颇',
  'title': '正义爆轰',
  'new_type': 0,
  'hero_type': 3,
  'skin_name': '正义爆轰|地狱岩魂'},
 {'ename': 106,
  'cname': '小乔',
  'title': '恋之微风',
  'new_type': 0,
  'hero_type': 2,
  'skin_name': '恋之微风|万圣前夜|天鹅之梦|纯白花嫁|缤纷独角兽'},
......

但是针对第一个英雄廉颇点开详情页后,可以看到网址为https://pvp.qq.com/web201605/herodetail/105.shtml,从而可以知道页面随着ename变化。

我们发现实际并不只有这两个皮肤,勇者和特殊活动的皮肤并不直接在json接口里显示:

f93bd2c45865dae7f85d038cc9c76dc1.png

这意味着完整皮肤详情只能访问详情页查看,透过style属性可以看到图片地址,从而发现规律:

f0ab68b659ea2af2981cf0ac35c092c6.png

而完整的皮肤名称列表可以通过上述ul的data-imgname属性获取,最终完整代码如下:

import requests
import os
import re
from concurrent.futures import ThreadPoolExecutor


def download_img(eid, name, i, skin_name):
    filename = f"王者荣耀壁纸/{eid:0>3}-{name}-{i:0>2}-{skin_name}.jpg"
    print(filename)
    if os.path.exists(filename):
        return
    img_url = f"http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{eid}/{eid}-bigskin-{i}.jpg"
    res = requests.get(img_url)
    with open(filename, "wb") as f:
        f.write(res.content)


def download_hero_skin(hero):
    eid, name = hero["ename"], hero["cname"]
    res = requests.get(f"https://pvp.qq.com/web201605/herodetail/{eid}.shtml",
                       headers=headers)
    res.encoding = "gbk"
    skin_names = re.findall(
        '<ul[^>]+?data-imgname="([^"]+)"', res.text)[0].split("|")
    print(eid, name, skin_names)
    for i, skin_name in enumerate(skin_names, 1):
        end = skin_name.find("&")
        skin_name = skin_name[:len(skin_name) if end == -1 else end]
        download_img(eid, name, i, skin_name)


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
}
herolist = requests.get(
    "https://pvp.qq.com/web201605/js/herolist.json", headers=headers).json()
os.makedirs("王者荣耀壁纸", exist_ok=True)
with ThreadPoolExecutor(max_workers=16) as executor:
    executor.map(download_hero_skin, herolist)

经过30秒左右已经下载完毕:

79003937175e5a511c49cb20a3790c1d.png

不到40行代码就实现了在30秒内下载完200多MB的王者荣耀壁纸。

6f5714cea8b23fdf4b44b49af44ba6cc.png

d59ca300193e9f745dd383fe5b9bf505.png

扫码加好友,加入海归Python编程和人工智能群

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值