0
点赞
收藏
分享

微信扫一扫

python爬虫入门案例(爬取lol所有英雄名称及技能)

郝春妮 2022-03-11 阅读 39

滴滴滴,这几天忙着增强自己的实力,发现了一个非常适合新手的案例
案例就是爬取英雄联盟的所有英雄名称和技能,废话不多说,我们来分析分析
要练手的链接
进入此网站我们会发现,网页上的响应数据里面并没有我们想要的数据,由此我们可以猜测,该响应的数据格式为json数据
在这里插入图片描述
猜测是json数据格式的相应内容,我们可以进入xhr中寻找我们想要的数据包
我们会发现,里面有一个叫hero数据包,里面有我们想要的数据

在这里插入图片描述
然后我们来分析获取单个英雄的技能数据,同样的这个数据包也是json格式的

在这里插入图片描述
我们分析会发现我们需要上个json数据中的heroId并传给第二个json数据的网址中,就能得到相应的英雄技能,分析完毕代码如下:

"""
网址:https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js
目标:获取所有英雄的名称和技能名
数据结构:json
数据请求方式:ajax
"""
import requests

url  = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'

headers_ = {
'Referer': 'https://lol.qq.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',
}

response = requests.get(url=url,headers=headers_).json()
# print(response)
json_data = response['hero']
for j in json_data:
    heroId= j['heroId']
    hero_name = j['name']
    # print(hero_name)
    detail_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{heroId}.js'
    skill_response = requests.get(url=detail_url,headers=headers_).json()
    # print(skill_response['spells'])
    skills_list = skill_response['spells']
    ls_=[]
    for skill in skills_list:
        # print(skill['name'])
        skills = skill['name']
        ls_.append(skills)
    print("英雄名:",hero_name,"技能:",ls_)

不懂的可以私信博主哦,一起努力一起进步💪,(●ˇ∀ˇ●)

举报

相关推荐

0 条评论