前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取
基本环境配置
- python 3.6
- pycharm
- requests
网页分析
import requests
import pprint
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Referer': 'https://lol.qq.com/data/info-heros.shtml',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
}
response = requests.get(url=url, headers=headers)
html_data = response.json()
pprint.pprint(html_data)
解析网页数据
heroes = html_data['hero']
lis = []
for hero in heroes:
hero_id = hero['heroId']
lis.append(hero['heroId'])
for li in lis:
hero_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(li)
response = requests.get(url=hero_url, headers=headers)
hero_data = response.json()
skins = hero_data['skins']
for skin in skins:
time.sleep(1)
img_url = skin['mainImg']
name = skin['name']
xuancai_url = skin['chromaImg']
保存数据
response = requests.get(url=img_url, headers=headers)
with open('G:\\python\\demo\\案例\\英雄联盟\\img\\' + name + '.jpg', mode='wb') as f:
f.write(response.content)
print('{}下载完成'.format(name))
实现效果