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 ...
随机推荐
- .NET Core:Token认证
现在是WebAPI的时代,你所需要面对的不止是浏览器了,通常会使用Web, WebApp, NativeApp等多种呈现方式.其中诸如Ember,Angular,Backbone之类的前端框架类库正随 ...
- SqlServer 通过日志恢复数据库
前期工作 查看数据属性,确保下条件: 1.数据库属性->选项->恢复模式=完整 2.建好库以后.一个数据库完整的数据备份 3.到出事期间日志没有你间断 4.记录出事的准确时间 一.数据准备 ...
- Maven 的 classifier 的作用
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79733655 直接看一个例子,maven中要引入json包,于是使用了: < ...
- Unity Shader 广告牌效果
广告牌效果指的是,一个二维平面的法线方向始终与视线(摄像机的观察方向)相同.广泛运用于渲染烟雾,云朵,闪光等. 它的本质在于构建旋转矩阵,此时我们可以选择三个基向量来构建此矩阵. 指向→的方向(X轴) ...
- thinkphp3.2.3集成phpexcel1.8导出设置单元格合并
1 到这里下载classes里面的文件 https://github.com/PHPOffice/PHPExcel 2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexcel 然 ...
- Python 练习题:用索引取出LIST中的值
请用索引取出下面list的指定元素,分别为Apple,Python,Lisa # -*- coding: utf-8 -*- # 请用索引取出下面list的指定元素 L = [ ['Apple','G ...
- 排序算法Java代码实现(三)—— 插入排序 和 希尔排序
因为希尔排序的核心思想是插入排序,所以本篇将两篇排序一起记录 本篇内容: 插入排序 希尔排序 (一)插入排序 算法思想: 把n个待排序的元素看成一个有序表和一个无序表,开始时有序表中只有一个元素,无序 ...
- dotnet core 之 gRPC
dotnet core gRPC 原文在本人公众号中,欢迎关注我,时不时的会分享一些心得 HTTP和RPC是现代微服务架构中很常用的数据传输方式,两者有很多相似之处,但是又有很大的不同.HTTP是一种 ...
- File "tesserocr.pyx", line 2443, in tesserocr._tesserocr.image_to_text RuntimeError: Failed to in...
将Tesseract-OCR安装目录下的tessdata文件夹复制到Python解释器目录下就可以了
- Vue入门篇
Vue-cli开发环境搭建 1. 安装nodejs 2. 设置缓存文件夹 $ npm config set cache "D:\vueProject\nodejs\node_cache&qu ...