import requests
import re
import os
from lxml import etree
def ck_dir():
pic_dir = 'pics'
if not os.path.exists(pic_dir):
os.mkdir(pic_dir)
os.chdir(pic_dir)
def main():
ck_dir()
headers = {'user-agent': 'firefox'}
count = 1
port_url = 'https://pvp.qq.com/web201605/herolist.shtml'
r = requests.get(port_url, headers=headers)
r.encoding = 'gbk'
html = etree.HTML(r.text)
# 获取英雄主页列表
li_list = html.xpath('//ul[contains(@class,"herolist") and contains(@class,"clearfix")]/li')
# 遍历列表
for li in li_list:
# 获取英雄主页资源url
h_url = li.xpath('./a/@href')[0]
# 合成访问url
port_url = f'https://pvp.qq.com/web201605/{h_url}'
r = requests.get(port_url, headers=headers)
# 获取图片base 路径
p_url = re.findall(r'(game.*?bigskin-)1.jpg', r.text)[0]
# 统计皮肤数目,url数据等于pic数
p_num = len(re.findall(r'pf_pic[0-9].jpg', r.text))
# 下载所有皮肤
for jtem in range(1, p_num + 1):
# 用base路径+数目合成图片url
url = f'https://{p_url}{jtem}.jpg'
# print(url)
# 取名
p_name = url.split('/')[-1]
r = requests.get(url, headers=headers, timeout=5)
with open(p_name, 'wb') as f:
f.write(r.content)
print(f'已下载{p_name},共下载{count}张图片。')
count = count + 1
if __name__ == '__main__':
main()
|