我爱it学习

 找回密码
 立即注册
搜索
查看: 65|回复: 0

[Python] 王者荣耀壁纸爬取

[复制链接]

8

主题

2

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2025-6-17 15:24:30 | 显示全部楼层 |阅读模式
#导入相关库
import re
import requests
from bs4 import BeautifulSoup
import os
import urllib3
# 禁用 InsecureRequestWarning 警告,防止报错误
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# 建立保存路径
path = r'd:\王者荣耀壁纸'

# 下载函数
def download(url, path, name, hero_name):
    hero_dir = os.path.join(path, hero_name)
    if not os.path.exists(hero_dir):
        os.makedirs(hero_dir)
    try:
        response = requests.get(url, verify=False)
        response.raise_for_status()   #报错4xx——5xx时报错
        with open(os.path.join(hero_dir, f"{name}.jpg"), "wb") as f:
            f.write(response.content)
            print(f"已下载 《{name}》 图片")
    except requests.RequestException as e:
        print(f"下载图片失败:{e}")

# 单个英雄的所有皮肤图片URL、名字、编号
def get_single_hero_list(hero_id, hero_name, id_name):
    hero_url = f"https://pvp.qq.com/web201605/herodetail/{id_name}.shtml"
    try:
        response = requests.get(hero_url, verify=False)
        response.raise_for_status()#报错4xx——5xx时报错
        response.encoding = "gbk"      #转码
        soup = BeautifulSoup(response.text, "html.parser")
        name_data = soup.find("ul", class_="pic-pf-list pic-pf-list3").get("data-imgname")
        name_list = re.sub(r'&\d+', '', name_data)  # 使用r前缀修正正则表达式
        hero_name_list = name_list.split("|")

        for num, name in enumerate(hero_name_list):  #枚举法:数字和皮肤名字同时提取
            num += 1
            url = f"https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{num}.jpg"
            download(url, path, name, hero_name)
    except requests.RequestException as e:
        print(f"获取英雄详情失败:{e}")
    except AttributeError as e:
        print(f"解析英雄详情页面失败:{e}")

# 获取所有英雄目录
def get_main():
    hero_list_url = "https://pvp.qq.com/web201605/js/herolist.json"
    try:
        response = requests.get(hero_list_url, verify=False)
        response.raise_for_status()  #报错4xx——5xx时报错
        heroes = response.json()

        for hero in heroes:
            hero_id = hero["ename"]
            hero_name = hero["cname"]
            id_name = hero.get("id_name", hero_id) # 如果没有"id_name",使用"ename"
            get_single_hero_list(hero_id, hero_name, id_name)
    except requests.RequestException as e:
        print(f"获取英雄列表失败:{e}")

if __name__ == '__main__':
    get_main()
    print("下载完成")
#读取顺序为:

# 1 :if __name__ == '__main__':

# 2 :获取所有英雄目录

# 3 :单个英雄的所有皮肤图片URL、名字、编号

# 4 :下载函数

#该页面在下载好库后,在d盘下建立一个 王者荣耀壁纸 文件即可全部复制粘贴运用
免责声明:
1、论坛里的文章仅代表作者本人的观点,与本网站立场无关。出于遵守国家相关法规或促进论坛发展的前提,我们有权在不经作者准许的情况下删除其在【我爱it学习】所发表的文章。
2、论坛的所有文章、内容、信息、资料,都不保证其准确性、完整性、有效性、时效性。请依据情况自身做出判断。因阅读本站内容而被误导等其他因素所造成的损失责任自负。【我爱it学习】不承担任何责任。
3、坛友对自己的言论和行为负责,完全承担发表内容的责任,所持立场与【我爱it学习】论坛无关。论坛使用者因为任何行为而触犯中华人民共和国法律或相关法规的,一切后果自己负责,【我爱it学习】不承担任何责任。
4、坛友所发布的信息中涉及到具体的第三方个人(单位/公司)隐私、商业秘密等,侵犯其权益,对其构成不良影响的,由第三方向【我爱it学习】提交正式书面申请删除该信息后,【我爱it学习】有权将该信息予以直接删除处理。
5、如因系统维护或升级而需暂停服务时,将事先公告。若因线路及非本站点控制范围外的硬件故障或其它不可抗力而导致暂停服务,于暂停服务期间造成的一切不便与损失,【我爱it学习】不负任何责任。
6、凡以任何方式登陆本站或直接、间接使用【我爱it学习】论坛资料者,视为自愿接受【我爱it学习】论坛总规则的约束。本声明未涉及的问题参见国家有关法律法规,当本声明与国家法律法规冲突时,以国家法律法规为准。
7、【我爱it学习】所发布的一切文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理,Mail To: [email protected]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

侵权投诉|Archiver|小黑屋|我爱it学习

GMT+8, 2025-7-2 09:32

Powered by Discuz!

© 2001-2023 52itstudy.

快速回复 返回顶部 返回列表