day16-爬虫作业

import requests, os

# 获取全部英雄信息
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
headers = {
    'cookie': 'isActDate=19443; PTTactFirstTime=1679875200000; eas_sid=l14697T9u9P0z805x6r1U396I4; isHostDate=19443; PTTuserFirstTime=1679875200000; pgv_info=ssid=s4080084347; ts_last=lol.qq.com/data/info-heros.shtml; pgv_pvid=7421102125; ts_uid=1884593666; weekloop=0-0-0-13; gpmtips_cfg={"iSendApi":0,"iShowCount":0,"iOnlineCount":0,"iSendOneCount":0,"iShowAllCount":0,"iHomeCount":0}; lolqqcomrouteLine=data_data',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

response = requests.get(url, headers=headers)
response.encoding = 'gb2312'
result = response.json()

# print(result['hero'][0])

# 循环获取每个英雄信息
for x in result['hero']:
    # 获取英雄的ID
    heroId = x['heroId']
    # 获取每个英雄的地址
    url1 = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{heroId}.js'
    response1 = requests.get(url1, headers=headers)
    response1.encoding = 'gb2312'
    result1 = response1.json()

    # 给每个英雄创建一个文件夹
    os.mkdir(rf'./files/{x["name"]}')

    # 遍历英雄的每个皮肤
    for y in result1['skins']:
        # 获取皮肤的网络地址
        url_img = y['mainImg']
        # 判断地址是否为空
        if url_img:
            response_img = requests.get(url_img)
            result_img = response_img.content
            # 判断皮肤名字里面是否有'/'
            if '/' in y["name"]:
                y["name"] = y["name"].replace('/', '-')
            # 保存到对应文件夹
            with open(rf'./files/{x["name"]}/{y["name"]}.{url_img[-3:]}', 'wb') as f:
                f.write(result_img)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值