首页 > 编程语言 >Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤

Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤

时间:2022-11-13 17:34:08浏览次数:44  
标签:hero img Python 爬虫 url 英雄 skin data response


Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤

文章目录

  • ​​Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤​​
  • ​​背景:LOL这款游戏有着大量的玩家,这个游戏里面人们津津乐道的皮肤,每一款皮肤的出现都会引起一个轰动。这次我们就来用Python做一个简单的爬虫,对英雄里面的官网的英雄皮肤进行爬取。可以在自己的电脑上查看自己喜欢的英雄的皮肤​​
  • ​​第一步:​​
  • ​​第二步:找到url​​
  • ​​完成代码:​​
背景:LOL这款游戏有着大量的玩家,这个游戏里面人们津津乐道的皮肤,每一款皮肤的出现都会引起一个轰动。这次我们就来用Python做一个简单的爬虫,对英雄里面的官网的英雄皮肤进行爬取。可以在自己的电脑上查看自己喜欢的英雄的皮肤
第一步:

安装一些python库,也叫模块

import requests
import time #时间模块
import os #文件模块
第二步:找到url
url='https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
response=requests.get(url)
response_data=response.json()
print(response_data)

如图1:下面是官网英雄皮肤存放的json文件

Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤_json

url='https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
response=requests.get(url)
response_data=response.json()
print(response_data)
hero_list=response_data['hero']
for hero in hero_list:
heroId=hero['heroId']
heroName=hero['name']+"-"+hero['title']
print("英雄ID:"+heroId)
print("英雄名称:"+heroName)

如图2通过for循环对json文件进行解析:

Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤_官网_02

对英雄联盟皮肤的json进行进一步的解析:

hero_info_url="https://game.gtimg.cn/images/lol/act/img/js/hero/"+heroId+".js"
hero_info_response=requests.get(hero_info_url)
hero_info_data=hero_info_response.json()
skins=hero_info_data['skins']
for skin in skins:
skin_img_url=skin['mainImg']
skin_name=skin['name']
if skin_img_url:
skin_img_data=requests.get(skin_img_url).content#获取图片文件二进制的数据

爬取图片后面,我们要用一个文件夹进行存储我们爬到得一个图片:

path='E:\python社区版\python项目\Python爬虫学习案例\英雄联盟'#这个是我自己电脑创建用来存储图片文件夹
用 os模块进行一个对文件夹判断是否存在,如果不存在就创建一个文件夹
if not os.path.exists(path+heroName):
os.mkdir(path+heroName)

#对图片进行一个持久化得存储,放在文件夹里面
with open(fileName,'wb') as f:
f.write(skin_img_data)
print(skin_name+".....皮肤图片爬取完毕")

time.sleep(2)#调用python时间模块进行一个爬取时间得休眠,防止被网址由于爬取频率过快,被反爬
完成代码:
import requests
import time
import os

url='https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
response=requests.get(url)
response_data=response.json()
hero_list=response_data['hero']
path='E:\python社区版\python项目\Python爬虫学习案例\英雄联盟'
for hero in hero_list:
heroId=hero['heroId']
heroName=hero['name']+"-"+hero['title']
if not os.path.exists(path+heroName):
os.mkdir(path+heroName)
hero_info_url="https://game.gtimg.cn/images/lol/act/img/js/hero/"+heroId+".js"
hero_info_response=requests.get(hero_info_url)
hero_info_data=hero_info_response.json()
skins=hero_info_data['skins']
for skin in skins:
skin_img_url=skin['mainImg']
skin_name=skin['name']
if skin_img_url:
skin_img_data=requests.get(skin_img_url).content#获取图片文件二进制的数据
fileName=path+heroName+"/"+skin_name+".jpg"
with open(fileName,'wb') as f:
f.write(skin_img_data)
print(skin_name+".....皮肤图片爬取完毕")

time.sleep(2)

代码运行截图:

Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤_python_03


Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤_官网_04


Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤_官网_05


标签:hero,img,Python,爬虫,url,英雄,skin,data,response
From: https://blog.51cto.com/u_15870497/5847871

相关文章