python简单实现抓取英雄联盟皮肤原画:老玩家都哭了!

写在前面: 自学py已经快两个多月了吧,作为新手,就是敢于尝试,之前有看到有人抓取王者荣耀皮肤的,但是作为一个联盟老玩家,还是想搞一个抓取联盟皮肤的,下面分享一下我自己的学习经过,如果有错误或者建议,欢迎下面留言提出。

进入官网

抓取的网站如下:
英雄联盟资料库

然后我们通过开发者模式F12 ,可以看到箭头指向的一个文件
如果没看到的话,刷新一下页面就出来了

 url0="https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js"
    try:
        r=requests.get(url0)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        herolist=r.json()  #转换为josn格式
    except:
       print("爬取失败:")

然后我试着请求一下len(herolist),结果返回一个4,这就很纳闷了,按道理应该是147位英雄吧,接着直接打印出来herolist
这里只是截取部分打印信息:

{'hero': [{'heroId': '1', 'name': '黑暗之女', 'alias': 'Annie', 'title': '安妮', 'roles': 

现在就很清楚了,他是写在了"hero"里面,然后稍微修改了一下
代码如下

def getjosn():
    url0="https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js"
    try:
        r=requests.get(url0)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        herolist=r.json()  #转换为josn格式
    except:
       print("爬取失败:")
    print(herolist)
    herolists=herolist["hero"]    #获得hero对应的字典类型
    heronumber=list(map(lambda x:x["heroId"],herolists))  #获得英雄的编号列表
    heroname=list(map(lambda x:x["name"],herolists))   #获得英雄的名称列表
    return heroname,heronumber

通过这一步我们可以获得英雄的编号和名字,居然发现安妮是作为NO.1
果然亲女儿还是亲女儿!

接下来我们进入到皮肤原画的网址:

这里可以看到一个安妮的原画网址吧 高亮度显示的
但是这里要小心,不要用鼠标指向那个小安妮,那是获得窗口头像的,我们不要

安妮原画
https://game.gtimg.cn/images/lol/act/img/skin/big1000.jpg
然后看看安妮的第一个皮肤吧。
安妮第一个皮肤
https://game.gtimg.cn/images/lol/act/img/skin/big1001.jpg
对比这个二个皮肤网址就很容易发现区别了,就是后面从0到1,那我们试想第二个皮肤是不是从1到2呢,我们试了一下,结果是对的,依此类推,我们就可以获得安妮的所有皮肤了。
试着查看第二位英雄奥拉夫。
https://game.gtimg.cn/images/lol/act/img/skin/big2000.jpg
第三位呢?
https://game.gtimg.cn/images/lol/act/img/skin/big3000.jpg
我们得到规律,末尾的四位数,后三位是皮肤编号,前面一位以上的数字,可能就是英雄编号,那我们现在是不是得想起函数getjosn()返回的值呢?是不是可以很好的利用上去,完成我们的功能,我很高兴的去做了,结果并没有成功写入图片,原因是拼接图片的网址错了

之前我是直接拼接path,结果末尾四位居然出现了10,11,12这类问题
后面加入了一个strs()函数得到修改

path="https://game.gtimg.cn/images/lol/act/img/skin/big"+str(j)+str(k)+".jpg"

修改之后的代码:

def down():
    name,number=getjosn()   #获得name 和 编号   第一个黑暗之女   001    第二个狂战士 002
    i=0   #计数
    if not os.path.exists("D://英雄联盟壁纸//"):   #判断英雄联盟壁纸这个目录是否存在
        os.mkdir("D://英雄联盟壁纸//")    #创建这个目录
    for j in number:  #遍历每个编号
        if not os.path.exists("D://英雄联盟壁纸//"+name[i]):   #判断这个英雄文件夹是否存在
            os.mkdir("D://英雄联盟壁纸//"+name[i])   #创建这个英雄文件夹
        os.chdir("D://英雄联盟壁纸//"+name[i])  #进入这个文件夹
        i+=1
        for k in range(20):  #假设有20个皮肤
            path="https://game.gtimg.cn/images/lol/act/img/skin/big"+str(j)+str(strs(k))+".jpg"
            print(path)
            rr=requests.get(path)
            if rr.status_code==200: #请求正常
                with open(str(k)+".jpg","wb") as f:  #写入文件
                    f.write(rr.content)
                    f.close()
            print("{}的原壁纸已经下载了{}张\r".format(name[i-1],k))

其实这个程序到这里已经完成了,但是知识无极限,我还利用了空闲时间,让它实现了其他功能,现在附上全部代码:

# 编号 英雄 列表 url=https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js
import  requests
import  os
from  PIL  import  Image
import  time

def getjosn():
    url0="https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js"
    try:
        r=requests.get(url0)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        herolist=r.json()  #转换为josn格式
    except:
       print("爬取失败:")
    print(herolist)
    herolists=herolist["hero"]    #获得hero对应的字典类型
    heronumber=list(map(lambda x:x["heroId"],herolists))  #获得英雄的编号列表
    heroname=list(map(lambda x:x["name"],herolists))   #获得英雄的名称列表
    return heroname,heronumber
def strs(k):
    if k>=0 and k<10:
        return "00"+str(k)
    else:
        return "0"+str(k)
def down():
    name,number=getjosn()   #获得name 和 编号   第一个黑暗之女   001    第二个狂战士 002
    i=0   #计数
    if not os.path.exists("D://英雄联盟壁纸//"):   #判断英雄联盟壁纸这个目录是否存在
        os.mkdir("D://英雄联盟壁纸//")    #创建这个目录
    for j in number:  #遍历每个编号
        if not os.path.exists("D://英雄联盟壁纸//"+name[i]):   #判断这个英雄文件夹是否存在
            os.mkdir("D://英雄联盟壁纸//"+name[i])   #创建这个英雄文件夹
        os.chdir("D://英雄联盟壁纸//"+name[i])  #进入这个文件夹
        i+=1
        for k in range(20):  #假设有20个皮肤
            path="https://game.gtimg.cn/images/lol/act/img/skin/big"+str(j)+str(strs(k))+".jpg"
            print(path)
            rr=requests.get(path)
            if rr.status_code==200: #请求正常
                with open(str(k)+".jpg","wb") as f:  #写入文件
                    f.write(rr.content)
                    f.close()
            print("{}的原壁纸已经下载了{}张\r".format(name[i-1],k))
def look():
    hero=input("欢迎查询英雄皮肤,请选择你要查询的英雄:")
    m="D://英雄联盟壁纸//"+str(hero)
    if not os.path.exists(m):   #判断改英雄皮肤是否已经下载
        ys=input("很抱歉,这个英雄的壁纸暂时未下载,是否调入下载功能yes/no:")
        if ys=="yes":
            print("正在全部下载中:")
            down()
        else:
            print("请重新输入英雄:")
            look()
    for i in range(20):  #查看此英雄的所有皮肤壁画
        image=Image.open(m+"//"+str(i)+".jpg")
        image.show()
        time.sleep(1)

def main():
    print("该程序已经下载了部分英雄原壁纸(下载全部壁纸耗时且占用内存)")
    n=input("1、继续下载:/2、选择浏览:")
    if n=="1":
        down()
    else:
        look()
main()

这是大学生活中第一次写这个博客,好多东西不咋会,别喷我。对于这个代码来说,美中不足的是,我还没找到如何实现下载指定英雄的办法,这也是一个败笔 ,阅读代码如果有问题,或者有错误的话欢迎留言。

发布了11 篇原创文章 · 获赞 191 · 访问量 9533

猜你喜欢

转载自blog.csdn.net/qq_45906219/article/details/103612411