实现:自己输入英雄名,下载皮肤在文件夹内
难点:
- ajax和网页路径分析
英雄页面
#code是英雄对应的代码
hero_page_url = 'https://pvp.qq.com/web201605/herodetail/{0}.shtml'.format(code)
皮肤图片路径
'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{0}/{1}-bigskin-{2}.jpg'.format(code,code,i)
- 访问网页得到英雄列表json文件,并转为新的字典
关键语法,从旧字典中提取值组成新字典
hero_dict.update([(hero['cname'] ,hero['ename'])])
#hero_dict[hero['cname']] =hero['ename']
下贴详细代码
import requests
from lxml import etree
import time
import json
headers={
'referer': 'https://pvp.qq.com/web201605/herolist.shtml',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3754.400 QQBrowser/10.5.4034.400',
}
def hero_dict():
hero_dict = {}
hero_listurl = 'https://pvp.qq.com/web201605/js/herolist.json'
hero_list =requests.get(hero_listurl,headers= headers).content.decode('utf-8')
hero_list=json.loads(hero_list)
# print(hero_list)
for hero in hero_list:
hero_dict.update([(hero['cname'] ,hero['ename'])])
# print(hero_dict)
return hero_dict
def pic_url(code,name):
hero_page_url = 'https://pvp.qq.com/web201605/herodetail/{0}.shtml'.format(code)
hero_Message = requests.get(hero_page_url, headers=headers).content.decode('gbk')
hero_Html =etree.HTML(hero_Message)
pic_num =hero_Html.xpath('//div[@class="pic-pf"]/ul/@data-imgname')[0]
pic_names=pic_num.split('|')
for i in range(1,len(pic_names)+1):
pic_url ='https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{0}/{1}-bigskin-{2}.jpg'.format(code,code,i)
pic_cont = requests.get(pic_url, headers=headers).content
with open('皮肤\\{0}-{1}-{2}.jpg'.format(name,i,pic_names[i-1]),'wb') as ff:
ff.write(pic_cont)
time.sleep(1)
if __name__ == '__main__':
hero_dict()
hero_name =input('请输入英雄名:\n')
Hero_Dict = hero_dict()
if hero_name in Hero_Dict.keys():
hero_code = Hero_Dict[hero_name]
pic_url(hero_code,hero_name)
else:
print('英雄不存在')
改成爬取所有英雄皮肤,只需要将输入英雄名改为所有英雄名,存储英雄代码和名字的列表循环遍历即可
import requests
from lxml import etree
import time
import json
headers={
'referer': 'https://pvp.qq.com/web201605/herolist.shtml',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3754.400 QQBrowser/10.5.4034.400',
}
def hero_dict():
hero_dict = {}
hero_listurl = 'https://pvp.qq.com/web201605/js/herolist.json'
hero_list =requests.get(hero_listurl,headers= headers).content.decode('utf-8')
hero_list=json.loads(hero_list)
# print(hero_list)
for hero in hero_list:
hero_dict.update([(hero['cname'] ,hero['ename'])])
# print(hero_dict)
return hero_dict
def pic_url(code,name):
hero_page_url = 'https://pvp.qq.com/web201605/herodetail/{0}.shtml'.format(code)
hero_Message = requests.get(hero_page_url, headers=headers).content.decode('gbk')
hero_Html =etree.HTML(hero_Message)
pic_num =hero_Html.xpath('//div[@class="pic-pf"]/ul/@data-imgname')[0]
pic_names=pic_num.split('|')
for i in range(1,len(pic_names)+1):
pic_url ='https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{0}/{1}-bigskin-{2}.jpg'.format(code,code,i)
pic_cont = requests.get(pic_url, headers=headers).content
with open('皮肤\\{0}-{1}-{2}.jpg'.format(name,i,pic_names[i-1]),'wb') as ff:
ff.write(pic_cont)
print('皮肤\\{0}-{1}-{2}.jpg下载完毕'.format(name,i,pic_names[i-1]))
time.sleep(1)
if __name__ == '__main__':
hero_dict()
Hero_Dict = hero_dict()
for key in Hero_Dict:
hero_name = key
hero_code = Hero_Dict[hero_name]
pic_url(hero_code,hero_name)
网友评论