会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134712个问题
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 31楼
Python全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 32楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 33楼

image.png

Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 34楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 35楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 37楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 38楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 39楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 40楼
Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 42楼

http://codecampus.itbaizhan.com:8000/

无法访问:该网站未根据工信部相关法律规则在腾讯云进行备案。

Python全系列/第十六阶段:Python 爬虫开发/Python爬虫基础与应用 43楼
Python全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 44楼

发现有的皮肤名字是带着\\的,保存不到文件夹中,下面是改进的版本:

import requests

from fake_useragent import UserAgent

from urllib.request import urlretrieve

from os import makedirs, path

from time import sleep


def ensure_directory_exists(directory):

    if not path.exists(directory):

        makedirs(directory)


def sanitize_filename(filename):

    # 将文件名中的特殊字符替换为下划线

    return filename.replace(" ", "_").replace("/", "_").replace("\\", "_")


def get_hero_img(hero_id=1, hero_name=""):

    url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'

    headers = {'User-Agent': UserAgent().chrome}

    resp = requests.get(url, headers=headers)

   

    hero_dir = path.join('./_反爬和反反爬/imgs/lolskin', sanitize_filename(hero_name))

    ensure_directory_exists(hero_dir)


    for sk in resp.json().get('skins', []):

        if sk.get("mainImg"):

            print(f'皮肤名:{sk.get("name")}    皮肤下载地址: {sk.get("mainImg")}')

            img_filename = sanitize_filename(sk.get("name")) + '.jpg'

            img_path = path.join(hero_dir, img_filename)

            urlretrieve(sk.get("mainImg"), filename=img_path)

            sleep(0.5)


# ...(其余代码保持不变)


def get_hero_list():

    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'

    headers = {'User-Agent': UserAgent().chrome}

    resp = requests.get(url, headers=headers)

   

    for h in resp.json().get('hero', []):

        hero_id = h.get('heroId')

        hero_name = h.get('name')

        yield (hero_id, hero_name)


if __name__ == '__main__':

    for hero_id, hero_name in get_hero_list():

        print(f'=========正在下载的: 英雄名:{hero_name}')

        get_hero_img(hero_id, hero_name)

        print(f'=====下载完成:英雄:{hero_name}')


Python全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 45楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2026百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637