爬虫基础——爬取英雄联盟皮肤图片

简单的Ajax

Chrome浏览器的 开发者模式真是好用,F12大法是必备技能

源代码

# -*- encoding = utf-8 -*-
# D:\Program\Pycharm\Project
import requests
import json
import os


def get_id():
    idlist = []
    r = json.loads(requests.get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js').text)['hero']
    for item in r:
        idlist.append(item['heroId'])
    return idlist


def download(id):
    r = json.loads(requests.get(f'https://game.gtimg.cn/images/lol/act/img/js/hero/{id}.js').text)
    heroname = r['hero']['name']
    skin_url = []
    skin_name = []
    for item in r['skins']:
        skin_url.append(item['mainImg'])
        skin_name.append(item['name'])
    if not os.path.exists(heroname):
        os.mkdir(heroname)
    os.chdir(heroname)
    try:
        for l1, l2 in zip(skin_name, skin_url):
            with open('%s.jpg' % l1, 'wb') as f:
                f.write(requests.get(l2).content)
            print("%s 保存成功" % l1)
    except:
        pass
    os.chdir('..')


if __name__ == '__main__':
    if not os.path.exists('images'):
        os.mkdir('images')
    os.chdir('images')
    for item in get_id():
        download(item)

没有难度的程序,不多bb

发布了12 篇原创文章 · 获赞 15 · 访问量 2258

猜你喜欢

转载自blog.csdn.net/realmels/article/details/105607867
今日推荐