# 王者荣耀皮肤抓取
# URL:https://pvp.qq.com/web201605/herolist.shtml
# 需求:获取所有英雄的所有皮肤并下载到本地
# 思路:
# 1. 请求英雄列表页URL ---> 获取每一个英雄的URL
# 2. 请求每个英雄的URL ---> 获取对应英雄的皮肤URL
# 3. 请求每个英雄的皮肤URL ---> 保存
import requests
from lxml import etree
# 发起请求,接收响应
# response = requests.get(url='https://pvp.qq.com/web201605/herolist.shtml',headers=headers)
# print(response.content.decode('gbk'))
# 夏洛特:https://pvp.qq.com/web201605/herodetail/536.shtml
# 阿古朵:https://pvp.qq.com/web201605/herodetail/533.shtml
# 蒙犽:https://pvp.qq.com/web201605/herodetail/524.shtml
# 经过对比,发现,只有数字不同,每个英雄都有对应的数字
# 所以,找每个英雄的id
# 可以通过json文件,获取每个英雄的ename,ename对应每个英雄的id
# 皮肤规律:
# 廉颇:
# 1. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-3.jpg
# 2. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-2.jpg
# 3. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-1.jpg
# 赵云
# 1. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/107/107-bigskin-8.jpg
# 定义获取ename的函数
def get_ename(url):
ename_lst = []
# 发起请求,接收响应
response = requests.get(url=url,headers=headers)
content_list = response.json()
# 循环获取每一个英雄的ename
for content in content_list:
ename = content['ename']
ename_lst.append(ename)
return ename_lst
# 定义获取图片链接函数
def get_img_href(url):
# 发起请求,接收响应
response = requests.get(url=url,headers=headers)
html = etree.HTML(response.content.decode('gbk'))
# 获取皮肤数量
ul = html.xpath('//ul[@class="pic-pf-list pic-pf-list3"]/@data-imgname')[0]
pifu_list = ul.split('|')
return pifu_list
# 定义下载图片函数
def download_img(ename,pifu_list):
length = len(pifu_list)
for i in range(1,length+1):
img_response = requests.get(url=pifu_url.format(ename,ename,i))
# print(img_response)
# 保存图片
with open(pifu_list[i-1]+'.jpg','wb') as fp:
fp.write(img_response.content)
pass
# 定义主函数
def main():
ename_lst = get_ename(herolist_url)
for ename in ename_lst[0:2]:
pifu_list = get_img_href(hero_detail_url.format(ename))
download_img(ename,pifu_list)
pass
if __name__ == '__main__':
# 定义获取ename的URL
herolist_url = 'https://pvp.qq.com/web201605/js/herolist.json'
# 定义请求头字典
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
}
# 定义英雄详情页的URL
hero_detail_url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'
# 定义英雄皮肤的URL
pifu_url = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg'
main()
```
你想要的都在这里 王者荣耀 皮肤爬取
最新推荐文章于 2023-06-10 15:43:22 发布