python爬取图片作壁纸

闲来无事,爬取王者荣耀皮肤做桌面。顺便巩固下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


下载示意:

下载图片示意图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值