from requests_html import HTMLSession
session = HTMLSession()
import os
class Spider(object):
os_path = os.getcwd() + '/王者皮肤图片/'
if not os.path.exists(os_path):
os.mkdir(os_path)
def __init__(self):
'''
一、准备数据
'''
#请求地址
self.start_url= 'https://pvp.qq.com/web201605/herolist.shtml'
#请求头
self.headers ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
}
def parse_start_url(self):
'''
二、发送请求,获取响应
'''
response = session.get(url =self.start_url,headers =self.headers).html
src_url_list = response.xpath('//div[@class="herolist-content" ]/ul/li/a/@href')
hero_name_list =response.xpath('//div[@class="herolist-content" ]/ul/li/a/text()')
img_src =response.xpath('//div[@class="weapons-list"]/ul/li/a/img/@data-img')
for hero_info_url,hero_name in zip( src_url_list,hero_name_list):
#英雄的id
hero_id=hero_info_url[11:-6]
#英雄的详情页地址
hero_info_url ='https://pvp.qq.com/web201605/'+hero_info_url
#对英雄详情页发送请求
response =session.get(hero_info_url,headers =self.headers).html
self.parse_info_response_data(response, hero_id, hero_name)
def parse_info_response_data(self, reaponse, hero_id, hero_name) :
'''
三、解析响应,提取数据
'''
hero_name_1 = ' '.join(reaponse.xpath('//div[@class="pic-pf"]/ul/@data-imgname'))
a = hero_name_1.split('|')
hero_name_list =[]
for i in a:
hero_name_list.append(i.split('&')[0])
for num, name in enumerate(hero_name_list):
img_url =f'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{num+1}.jpg'
#获取图片二进制数据
data = session.get(img_url).content
self.parse_save_data(data, hero_name, name)
def parse_save_data(self, data, hero_name, name):
'''
四、保存数据
'''
#创建子文件夹
os_path =self.os_path+f"{hero_name}"
if not os.path.exists(os_path):
os.mkdir(os_path)
with open(os_path+name+'.jpg','wb')as f:
f.write(data)
print(f'{hero_name}皮肤:{name}图片-----下载完成!!!')
if __name__ == '__main__':
s=Spider()
s.parse_start_url()
案例:利用爬虫爬取王者皮肤图片
最新推荐文章于 2024-08-15 12:11:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)