python爬取全站壁纸代码
#测试网址:https://www.ivsky.com/bizhi/
#需要安装的库:requests,bs4
#本人是个强迫症患者,为了美观添加数个print(),其并没有实际意义,若是不爽删去即可。
import requests,re,os
from bs4 import BeautifulSoup
from time import sleep
from random import uniform
#网址解析
def url_open(url):
headers= {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
headers["Referer"] = "https://www.ivsky.com/bizhi/"
html = requests.get(url,headers=headers).text
return html
#获取全部主题图片链接
def get_url_all():
print("正在收集整理壁纸主题网址,请稍候.....")
print()
theme_url_list = []
theme_title_list = []
data = []
page_totle = 100 #壁纸主题共有100页
#逐页收集主题URL
for page in range(1,page_totle+1):
url = "https://www.ivsky.com/bizhi/index_{}.html".format(page)
html = url_open(url)
soup = BeautifulSoup(html,"html.parser")
url_all = soup.find_all("div",class_="il_img")
for each in url_all:
theme_title = each.a["title"]
theme_title_list.append(theme_title)
theme_url = "https://www.ivsky.com" + each.a["href"]
theme_url_list.append(theme_url)
#将数据打包 以便能够将两个数据一起返回
data.append(theme_url_list)
data.append(theme_title_list)
break #减少调试运行时间使用 若要获取全部主题链接则删除此处即可
theme_totle = len(data[0]) #计算主题数目
print("壁纸网址收集结束,共收集%d个主题,准备进行图片下载....."%theme_totle)
sleep(1) #走个形式而已
return data
def save_img(img_url_list,theme_name,work_path):
#更改图片保存路径(分主题保存)
save_path = work_path + r"\%s" % theme_name
if os.path.exists(save_path) == True:
os.chdir(save_path)
else:
os.mkdir(save_path)
os.chdir(save_path)
num = 0 #当前任务图片下载计数
for img_url in img_url_list:
num += 1
print("正在下载主题“%s”第%d张图片" % (theme_name, num))
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
content = requests.get(img_url, headers=headers).content
with open("%d.jpg" % num, "wb") as f:
f.write(content)
sleep_time = uniform(0.18,0.37) #随机休眠 减少服务器压力 (真有诚意调大点即可)
sleep(sleep_time)
def get_img(data):
img_root_url = "https://img.ivsky.com/img/bizhi/pre/"
num_1 = -1 # 标题索引 后面用于索引标题
work_path = os.getcwd()
num_2 = 0 #统计图片总张数
for theme_url in data[0]:
#print(theme_url)
num_1 += 1
theme_name_temp = data[1][num_1] #获取对应的主题名称
img_url_list = [] #用于存储单个主题的图片下载链接
#去掉(x张)字眼 (强迫症患者)
p_theme_name = r'(.+)[(]\d+?张[)]'
theme_name = re.findall(p_theme_name,theme_name_temp)[0]
print()
print("正在下载主题:%s"%theme_name)
print()
#每个页面16张图片 若主题图片数目大于16张图片则存在多个页面.....
p_img_num = r'.+[(](\d+?)张[)]'
img_num = int(re.findall(p_img_num,theme_name_temp)[0])
if img_num / 16 > img_num // 16:
page_totle = img_num // 16 + 1
else:
page_totle = img_num / 16
#获取全部图片链接
if page_totle == 1:
html = url_open(theme_url)
soup = BeautifulSoup(html,"html.parser")
soup_img_url = soup.find_all("div",class_="il_img")
for each in soup_img_url:
temp = each.img["src"].split("/t/")[1]
img_url = img_root_url + temp
img_url_list.append(img_url)
num_2 += 1
else:
for page in range(1,page_totle+1):
url = theme_url + "index_{}.html".format(page)
html = url_open(url)
soup = BeautifulSoup(html,"html.parser")
soup_img_url = soup.find_all("div",class_="il_img")
for each in soup_img_url:
temp = each.img["src"].split("/t/")[1]
img_url = img_root_url + temp
img_url_list.append(img_url)
num_2 += 1
save_img(img_url_list, theme_name,work_path) #图片下载保存
print()
print("任务完成,共计下载图片%d张"%num_2)
def main():
path = r'C:\Users\Administrator\Desktop\test'
if os.getcwd() != path:
if os.path.exists(path) == False:
os.mkdir(path)
os.chdir(path)
else:
os.chdir(path)
data = get_url_all()
get_img(data)
if __name__ == "__main__":
main()
python爬取全站壁纸代码的更多相关文章
- Python爬取全站妹子图片,差点硬盘走火了!
在这严寒的冬日,为了点燃我们的热情,今天小编可是给大家带来了偷偷收藏了很久的好东西.大家要注意点哈,我第一次使用的时候,大意导致差点坏了大事哈! 1.所需库安装 2.网站分析 首先打开妹子图的官网(m ...
- Python 爬取必应壁纸
import re import os import requests from time import sleep headers = { "User-Agent": (&quo ...
- python 爬取王者荣耀高清壁纸
代码地址如下:http://www.demodashi.com/demo/13104.html 一.前言 打过王者的童鞋一般都会喜欢里边设计出来的英雄吧,特别想把王者荣耀的英雄的高清图片当成电脑桌面 ...
- Python爬虫 - 爬取百度html代码前200行
Python爬虫 - 爬取百度html代码前200行 - 改进版, 增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...
- python爬取网页的通用代码框架
python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...
- 如何用python爬虫从爬取一章小说到爬取全站小说
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
- 使用python爬取MedSci上的期刊信息
使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...
- python爬取免费优质IP归属地查询接口
python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...
随机推荐
- Windows搭建FTP/Http文件共享(利用IIS)
控制面板——程序——添加功能 勾选Ftp服务器.万维网服务.IIS管理控制台 然后,计算机——右键管理——服务和应用程序,添加网站和添加Ftp IP设置为 未分配 或 本机获取到的静态IP即可. 然后 ...
- swagger Unable to render this definition
Unable to render this definition The provided definition does not specify a valid version field. Ple ...
- 【操作系统之十三】Netfilter与iptables
一.Netfilter Netfilter是由Rusty Russell提出的Linux 2.4内核防火墙框架,该框架既简洁又灵活,可实现安全策略应用中的许多功能,如数据包过滤.数据包处理.地址伪装. ...
- Java连载17-赋值类运算符&字符串的连接运算符
一.赋值运算符 1.赋值类运算符包括两种: (1)基本赋值运算符:= (2)扩展的赋值运算符: += -= *= /= &= 赋值类的运算符优先级:先执行等号右边的表 ...
- SpringBoot第十四篇:统一异常处理
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10984081.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 本文将谈论 ...
- Zookeeper在linux上的安装
1:进入 cd /usr/local目录下 2:创建zookeeper目录 midir zookeeper 3:将压缩包复制到zookeeper目录下 cp /root/zookeeper/zo ...
- dns攻击包代码实现
博客地址:http://home.cnblogs.com/u/zengjianrong/ 代码没有做好精简,有些多余的没有删去,因为博主太懒了哈哈 #include <stdio.h> # ...
- 【C++】Debug模式和Release模式的区别
VS中的程序有两种编译模式:Debug模式和Release模式. Debug通常称为调试版本,通过一系列编译选项的配合,编译结果通常包含调试信息,而且不做任何优化,以为开发人员提供强大的应用程序调试能 ...
- Appium+python自动化(一)- 环境搭建—上(超详解)
简介 今天是高考各地由于降水,特别糟糕,各位考生高考加油,全国人民端午节快乐.最近整理了一下自动化的东西,先前整理的python接口自动化已经接近尾声.即将要开启新的征程和篇章(Appium& ...
- mysqlbinlog恢复误删数据
概述 代码bug,在处理上传出现异常时执行了DELETE FROM t_resource WHERE resource_id = ? OR parent_id = ?因为OR条件导致用户的上传的所有数 ...