小朋友们好,大朋友们好!
我是猫妹!

哈哈哈,又到周末啦!
这周过得怎么样?

马上就要开学了,寒假作业早已写好了吧?
开学让人兴奋,上了很久网课都要吐啦!
开学也让人有点小焦虑,不能睡懒觉啦!

平时还是有点小爱好比较好,比如Python,给生活加点乐趣!
今天聊聊用Python实现壁纸下载,本文有很多漂亮壁纸哦!

轻松搞定100张壁纸
先看效果,感兴趣,再继续看。
这是猫妹从网上下载的100张壁纸,如果想要更多,修改个参数即可。

我们打开几张动物为主题的壁纸欣赏下吧!
原图很清晰,需要原图的话可以自行下载,或联系猫妹。




这会儿,已经有不少小伙伴亟不可待了
代码呢?
怎么实现的?
看这里:
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

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










免费群、收费群
猫妹计划组建一个免费群,一个收费群。
面向小朋友等初学者,提高动手能力。
你有什么建议吗?
感兴趣可以主动联系猫妹哦!

好了,今天的分享就到这里,谢谢朋友们的阅读。
我是猫妹,咱们下次见!