使用Python爬取王者榮耀全英雄頭像

王者榮耀官網英雄列表:


使用Python爬取王者榮耀全英雄頭像

爬取結果


使用Python爬取王者榮耀全英雄頭像

爬取過程


使用Python爬取王者榮耀全英雄頭像

爬取代碼

爬取的代碼在這裡就直接給大家分享出來,喜歡的朋友,可以關注、收藏、點贊、轉發。

<code>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("D:/圖庫\\圖/王者榮耀英雄頭像/%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)/<code>


分享到:


相關文章: