小朋友们好,大朋友们好!
我是猫妹!
![](https://img-blog.csdnimg.cn/img_convert/c2e0ac8fe3e4595fb00b41d38d5c61bc.jpeg)
哈哈哈,又到周末啦!
这周过得怎么样?
![](https://img-blog.csdnimg.cn/img_convert/bc840c1e93f4c2d1de862a3ce96ac4fd.jpeg)
马上就要开学了,寒假作业早已写好了吧?
开学让人兴奋,上了很久网课都要吐啦!
开学也让人有点小焦虑,不能睡懒觉啦!
![](https://img-blog.csdnimg.cn/img_convert/0f3db69eae896fcb77ce8d83cffd5c2d.jpeg)
平时还是有点小爱好比较好,比如Python,给生活加点乐趣!
今天聊聊用Python实现壁纸下载,本文有很多漂亮壁纸哦!
![](https://img-blog.csdnimg.cn/img_convert/d80b89f46d85fd5ae33da79bbcbd0b9c.jpeg)
轻松搞定100张壁纸
先看效果,感兴趣,再继续看。
这是猫妹从网上下载的100张壁纸,如果想要更多,修改个参数即可。
![](https://img-blog.csdnimg.cn/img_convert/eec3208bc82881b9ff18ec5f90524068.png)
我们打开几张动物为主题的壁纸欣赏下吧!
原图很清晰,需要原图的话可以自行下载,或联系猫妹。
![](https://img-blog.csdnimg.cn/img_convert/22dfce44504e429346e9c672b889658b.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/922632ba6cb6345399089b87706a1640.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/905fb745f052363f5ea90478302560a6.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/3606809e3abf1828c01f1949ebc6492c.jpeg)
这会儿,已经有不少小伙伴亟不可待了
代码呢?
怎么实现的?
看这里:
import requestsfrom pathlib import Pathdefsave_pic(path:Path):for i in range(100): print('*',end=' ') url = f"http://bingw.jasonzeng.dev?resolution=UHD&index={i}"with requests.get(url) as r:with open(path/f"{i}.jpg","wb") as w: w.write(r.content)if __name__ == "__main__": saved_path = Path("./和猫妹学Python") saved_path.mkdir(parents = True, exist_ok = True) print('下载中') save_pic(saved_path) print('\n下载完成')
代码就是这么简洁,逻辑也不复杂。
建立一个文件夹
从网上特定API下载图片
下载完成
关键代码
代码用到了两个库Path和requests。
Path
Path模块提供表示文件系统路径的类,其语义适用于不同的操作系统。
我们这里知道它是建立了一个文件夹即可。
Path API详解见:
https://path.readthedocs.io/en/latest/api.html
requests
requests是大名名鼎的爬虫库。
如果没有安装requests库,先用pip install指令安装下requests库。
我们是通过爬虫来下载壁纸的,所谓爬虫就是用代码解放了我们的双手。
我们先用双手下载一张壁纸试试,随便打开一个网址,图片另存为即可。
http://bingw.jasonzeng.dev?resolution=UHD&index=1
![](https://img-blog.csdnimg.cn/img_convert/979ed8d52061ee508960dbe83a30316b.png)
resolution指定壁纸图像的分辨率,默认1920x1080,取值如下:
UHD,高清
1920x1200
1920x1080
1366x768
1280x768
1024x768
800x600
800x480
768x1280
720x1280
640x480
480x800
400x240
320x240
240x320
index表示获取哪一天的图片,0 表示今天,1 表示昨天,以此类推,index=random 表示随机一天。
date表示获取从某某一天到今天的图片,比如 data=20230211。
http://bingw.jasonzeng.dev?resolution=UHD&data=20230211
w和h表示指定图片的宽度和高度。
http://bingw.jasonzeng.dev?w=240&h=320&data=20230211
qlt表示指定图片的质量,取值范围是 0 到 100,100最清晰
http://bingw.jasonzeng.dev?qlt=100&data=20230211
![](https://img-blog.csdnimg.cn/img_convert/14990a9feaa17c8a3ad6e249e9a45370.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/044d7dba6236f4635dcded1884160eff.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/11f483f4b184f89358d93077f752c3a8.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/d545f75438c84734334d83c3b9efdba7.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/891247bee770f28ef6f4db96adfd684b.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/9a505f7c0d0226c706c0d1c7d422af4e.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/377c80692a6f5dfdd2db792a047a0193.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/d74f4e00d2fa64c0eb271732f3c8c417.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/aeadb10c3ed310201fd67b4f8df77e21.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/e1116ae1f13ea1c7dee6702586158035.jpeg)
免费群、收费群
猫妹计划组建一个免费群,一个收费群。
面向小朋友等初学者,提高动手能力。
你有什么建议吗?
感兴趣可以主动联系猫妹哦!
![](https://img-blog.csdnimg.cn/img_convert/f02288c79e657af5da25f27ea2a3904e.jpeg)
好了,今天的分享就到这里,谢谢朋友们的阅读。
我是猫妹,咱们下次见!