python爬虫爬取王者荣耀官网全部英雄头像(源码分享)

这篇文章为源码分享,爬虫的分析过程请阅读文章

  • 《用python爬取王者荣耀官网的英雄皮肤海报(含源码)》

源码

import requests
import json
from bs4 import BeautifulSoup

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5958.400 SLBrowser/10.0.3533.400'}


#获取json数据,并解析成存有英雄id与名字的列表[[id,name],[id1,name1].......]
def jsonToHeroInfoList(jsonURL):
    resp  =requests.get(jsonURL)
    jsonData = json.loads(resp.text)
    heroInfoList = []
    for data in jsonData:
        hero_info =[]
        hero_info.append(data['ename'])
        hero_info.append(data['cname'])
        heroInfoList.append(hero_info)
    return heroInfoList

# 通过官网可知,英雄头像的src是: "http://game.gtimg.cn/images/yxzj/img201606/heroimg/%s/%s.jpg"%(HeroId,HerpId)


def downloadIMG(herInfoList):
    for herInfo in herInfoList:
        _id = herInfo[0]
        name = herInfo[1]
        src =  "http://game.gtimg.cn/images/yxzj/img201606/heroimg/%s/%s.jpg"%(_id,_id)
        resp = requests.get(src, headers=headers)
        print("正在下载: "+name+" 头像......")
        with open("C:/users/TTODS/desktop/王者荣耀/头像/%s.jpg"%name,'wb') as f:
            f.write(resp.content)

Json_url = "https://pvp.qq.com/web201605/js/herolist.json"
heroInfoList = jsonToHeroInfoList(Json_url)
downloadIMG(heroInfoList)

爬取结果

python爬虫爬取王者荣耀官网全部英雄头像(源码分享)_第1张图片

你可能感兴趣的:(python爬虫爬取王者荣耀官网全部英雄头像(源码分享))