Python使用json接口爬取网页数据

Python使用json接口爬取网页数据

示例:英雄联盟皮肤下载

"""
long long away
Date:2022/5/17 16:37
"""
import requests,json,os
from tqdm import tqdm

response = requests.get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js')
result = response.json()

name_list = []
for i in result['hero']:
    name_list.append(i['title'])
# print(name_list)
# print(len(name_list))
for i in tqdm(range(1,160)):

    os.mkdir(f'./files/英雄皮肤信息/{name_list[i-1]}')
    resp1 = requests.get(f'https://game.gtimg.cn/images/lol/act/img/js/hero/{i}.js')
    resp = resp1.json()

    print(i)
    for j in resp['skins']:
        name = j['name']
        if j['mainImg'] != "":
            URL = j['mainImg']
            resp2 = requests.get(url=URL)
            with open(rf'./files/英雄皮肤信息/{name_list[i - 1]}/{name}.jpg', 'wb') as f3:
                f3.write(resp2.content)
        # 炫彩皮肤
        # else:
        #     URL = j['chromaImg']
        #     resp2 = requests.get(url=URL)
        #     with open(rf'./files/英雄皮肤信息/{name_list[i - 1]}/{name}.png', 'wb') as f3:
        #         f3.write(resp2.content)




你可能感兴趣的:(python学习,python,爬虫)