闲来无事,爬取王者荣耀皮肤做桌面。顺便巩固下python函数,哈哈哈
#coding = utf-8
import requests
from bs4 import BeautifulSoup
import re
import os
hero_url_all = "https://pvp.qq.com/web201605/herolist.shtml"#所有英雄列表url
header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Chrome/65.0.3325.181'}
hero_all = requests.get(hero_url_all,headers=header)
hero_all.encoding = "gbk"
soup = BeautifulSoup(hero_all.text,"lxml")
all_url = soup.find_all("ul",class_ = "herolist clearfix")#所有英雄的网址html
path = "./皮肤/"
isExists =os.path.exists(path)#判断文件夹是否存在
if not isExists:
os.mkdir(path)
print(path + ' 创建成功')
else:
print(path + ' 目录已存在')
for hero in all_url:#遍历每个英雄的网址
for a in hero.find_all("a"):#这个英雄名称,用于创建皮肤图片文件名
url = "https://pvp.qq.com/web201605/"+a["href"]
request_hero = requests.get(url,headers=header)
soup = BeautifulSoup(request_hero.text,"lxml")
pifu_all = soup.find_all("div",class_ = "zk-con1 zk-con")
for i in pifu_all:#遍历这个英雄所有皮肤地址
findall = re.findall(r'[(](.*?)[)]', i["style"])#提取括号里的字符串,输出列表
pifu1 = findall[0].replace("'",'')[0:-5]#把字符串里的引号用空值替换
for i in range(1,10):#遍历该英雄皮肤数量上限,每个输出一个url
all_pifu ="http:"+pifu1+str(i)+".jpg"
pifu_down = requests.get(all_pifu,headers=header)#每个皮肤url地址
if pifu_down.status_code != 200:#判断皮肤网址是否超出上限
continue
else:
print("正在下载"+str(a.text)+"皮肤")
with open("./皮肤/"+str(a.text)+str(i)+".jpg","wb") as e:#下载每个皮肤图片
e.write(pifu_down.content)
print("正在下载"+str(a.text)+str(i)+".jpg")
if os.path.exists("./皮肤/"+str(a.text)+str(i)+".jpg"):#判断是否有重复皮肤图片
print("该皮肤已经存在")
else:
continue
下载示意:
下载图片示意图: