python爬取英雄联盟官网,获取所有英雄与皮肤!

python爬取英雄联盟官网,获取所有英雄与皮肤!

起因:练习一下python,看到有人爬取王者荣耀的皮肤,我也来练练手。

首先查看英雄联盟英雄界面的js,还有英雄的js以及皮肤图片的url。这里我们可以看到英雄的列表是在


                学习Python中有不明白推荐加入交流群
                号:516107834
                群里有志同道合的小伙伴,互帮互助,
                群里有不错的学习教程!

 

http://lol.qq.com/biz/hero/champion.js 中 

进入英雄界面,并查看元素

这里我们可以看到图片的

python爬取英雄联盟官网,获取所有英雄与皮肤!

http://osswebimg.qq.com/images/lol/web201310/skin/big266001.jpg在这里图片的id是266001 

其中266为英雄的id,而001指的是第一个。

在观察页面元素看到安妮的皮肤id和名称是在

扫描二维码关注公众号,回复: 4049253 查看本文章
http://lol.qq.com/biz/hero/Annie.js

中,至此完成了皮肤id和皮肤名称的获取。

python爬取英雄联盟官网,获取所有英雄与皮肤!

现在开始说明思路:

获取英雄的列表。

# 获取英雄联盟英雄列表
response = urllib.request.urlopen(
 "http://lol.qq.com/biz/hero/champion.js")
data = response.read().decode('utf-8')
json1 = re.findall(r"LOLherojs.champion=(.+?);", data)
hero_json = json.loads(json1[0])['keys']

获取英雄的皮肤id和名字。

c = []
for key in hero_json:
 # print("****key--:%s value--: %s" % (key, hero_json[key]))
 url_skin = "http://lol.qq.com/biz/hero/" + hero_json[key] + ".js"
 c.append(url_skin)

拼接出图片的url并下载。

 urlDown = "http://ossweb-img.qq.com/images/lol/web201310/skin/big" + imgId + ".jpg"

下面是全部的代码

# 代码片段1
import urllib.request
import json
import os
import pathlib
import re
from bs4 import BeautifulSoup
# 获取ID和name,并下载
def json_txt(jsonSkinSJSON, defalut):
 jsonSkinSJSON = jsonSkinSJSON["data"]["skins"]
 i = 0
 imgId = ""
 imgName = ""
 for key in jsonSkinSJSON:
 if i == 0:
 # print(key["id"])
 imgId = key["id"]
 # print(defalut)
 imgName = defalut
 i = i + 1
 else:
 imgId = key["id"]
 imgName = key["name"]
 save_dir = 'D:LOLheroskin\'
 save_file_name = save_dir + imgName + ".jpg"
 urlDown = "http://ossweb-img.qq.com/images/lol/web201310/skin/big" + imgId + ".jpg"
 # print(urlDown)
 try:
 if not os.path.exists(save_file_name):
 urllib.request.urlretrieve(urlDown, save_file_name)
 except Exception:
 print("下载失败")
# 获取英雄联盟皮肤
def getSkins(urlOne):
 response = urllib.request.urlopen(
 urlOne)
 data = response.read().decode('utf-8')
 # print(data)
 jsonSkin = re.findall(r"{"data":(.+?);", data)
 jsonSkinS = "{"data":" + jsonSkin[0]
 jsonSkinSJSON = json.loads(jsonSkinS)
 # print(jsonSkinSJSON["data"]["name"])
 defalut = jsonSkinSJSON["data"]["name"]
 json_txt(jsonSkinSJSON, defalut)
# 获取英雄联盟英雄列表
response = urllib.request.urlopen(
 "http://lol.qq.com/biz/hero/champion.js")
data = response.read().decode('utf-8')
json1 = re.findall(r"LOLherojs.champion=(.+?);", data)
hero_json = json.loads(json1[0])['keys']
c = []
for key in hero_json:
 # print("****key--:%s value--: %s" % (key, hero_json[key]))
 url_skin = "http://lol.qq.com/biz/hero/" + hero_json[key] + ".js"
 c.append(url_skin)
# 文件夹不存在则创建
save_dir = 'D:LOLheroskin\'
if not os.path.exists(save_dir):
 os.mkdir(save_dir)
for heroOne in c:
 getSkins(heroOne)
print("下载完成")

猜你喜欢

转载自blog.csdn.net/qq_41841569/article/details/83959438