最近发现Bing搜索的背景图片太适合当桌面背景了,一天一个还不带重样的,哈哈
1.获取地址
首先分析下Bing中获取背景图的请求:
发现是POST请求,而且还有很多不知名参数,还有防盗链什么的,显然不能为我们所用。
又去网上找了找,发现可以用新浪提供的一个映射:
https://area.sinaapp.com/bingImg/
下面就是实现了
2.代码实现
请求映射地址,获取跳转到Bing的实际 img 地址
def get_bing_images_url():
try:
# 获取实际地址
result = requests.get(BING_IMAGES_URL)
img_url = result.url
except Exception as e:
print('Bing地址请求异常:', e, '\n')
return False
else:
return img_url
保存下载的Bing图片
判断路径存在不存在,不存在时就建立
然后根据时间命名图片,毕竟一天一张一个日期正好
def save_bing_image(img_url, save_path):
try:
if not os.path.exists(save_path):
print('图片保存提示:文件夹', save_path, '不存在,重新建立。', '\n')
os.mkdir(save_path)
# 设置图片文件名称
baseName = time.strftime("%Y-%m-%d", time.localtime()) + '.jpg'
# 获得图片路径
filePath = os.path.join(save_path, baseName)
# 下载图片并保存
urllib.request.urlretrieve(img_url, filePath)
except IOError as e:
print('图片保存错误:文件操作失败。', e, '\n')
except Exception as e:
print('图片保存错误:', e, '\n')
else:
print('图片保存成功:', filePath, '\n')
return filePath
设置保存的图片为壁纸
def set_img_as_wallpaper(filePath):
ctypes.windll.user32.SystemParametersInfoW(20, 0, filePath, 0)
3.完整实现代码
import requests
import os
import time
import urllib.request
import ctypes
BING_IMAGES_URL = "https://area.sinaapp.com/bingImg/"
SAVE_PATH = "D:\\Download\\Bing"
retryNum = 0
allowTryNum = 2
waitTime = 60
# 测试网络连通性
def test_network_live(testUrl):
try:
urllib.request.urlopen(testUrl)
return True
except:
return False
# 请求网页,跳转到Bing的实际 img 地址
def get_bing_images_url():
try:
# 获取实际地址
result = requests.get(BING_IMAGES_URL)
img_url = result.url
except Exception as e:
print('Bing地址请求异常:', e, '\n')
return False
else:
return img_url
# 保存下载的Bing图片
def save_bing_image(img_url, save_path):
try:
if not os.path.exists(save_path):
print('图片保存提示:文件夹', save_path, '不存在,重新建立。', '\n')
os.mkdir(save_path)
# 设置图片文件名称
baseName = time.strftime("%Y-%m-%d", time.localtime()) + '.jpg'
# 获得图片路径
filePath = os.path.join(save_path, baseName)
# 下载图片并保存
urllib.request.urlretrieve(img_url, filePath)
except IOError as e:
print('图片保存错误:文件操作失败。', e, '\n')
except Exception as e:
print('图片保存错误:', e, '\n')
else:
print('图片保存成功:', filePath, '\n')
return filePath
# 设置保存的图片作为壁纸
def set_img_as_wallpaper(filePath):
ctypes.windll.user32.SystemParametersInfoW(20, 0, filePath, 0)
# 网络不通进行等待
def judge_wait_retry_real_url():
realImageUrl = False
for i in range(retryNum, allowTryNum):
if test_network_live(BING_IMAGES_URL) and retryNum <= allowTryNum:
realImageUrl = get_bing_images_url()
break
else:
time.sleep(waitTime)
return realImageUrl
def bing():
print('Bing每日一图将被保存在:', SAVE_PATH, '\n')
imgUrl = judge_wait_retry_real_url();
print('Bing每日一图地址为:', imgUrl, '\n')
if imgUrl != False:
imgPath = save_bing_image(imgUrl, SAVE_PATH)
set_img_as_wallpaper(imgPath)
if __name__ == '__main__':
bing()
# 避免程序运行完就结束
# os.system("pause")
4.打包成exe
1).安装pyinstaller
首先需要安装pyinstaller,当然也可以使用py2exe等其他工具都可以
pip install pyinstaller
2).使用pyinstaller
pyinstaller 最重要的两个参数就是 -F 与 -D 参数。
使用 - F 参数, pyinstaller 会将 python 程序打包成单个可执行文件。
使用 - D 参数, pyinstaller 会将 python 程序打包成一个文件夹,运行程序时,需要进入该文件夹,点击运行相应的可执行程序。
为了美观,还可以通过 - i 参数指定打包程序的图标 (icon),但这个命令只能在 Windows 平台下生效,此外还可以使用 - n 参数指定生成打包文件的名称。
如果你使用了 PyQt5 或 tkinter 开发了界面,通常不会希望程序运行时弹出 cmd 命令行,此时就可以使用 - w 参数。
简单总结一下:
-F:打包 Python 程序为单个可执行文件
-D:打包 Python 程序为一个文件夹
-i:生成图标,只适用于 Windows 平台
-n:指定打包后生成文件的名称
-w:禁止命令行弹出
-i 参数后必须接 .ico 结尾的图标文件
-D 或 -F 后必须接 python 程序的入库程序,常见情况为 main.py
对应依赖比较多的程序,建议使用 -D, -F 更适合单文件的 py 脚本
3).制作ico图标
ico图标就是我们在windows桌面看到的程序图标,
制作网站可以选 比特虫 或者 在线工具 界面简单还没有广告,值得拥有
首先上传文件,然后选择大小,再数据验证码即可。
4).打包
使用下面命令打包
pyinstaller -D bingDailyPhoto.py -i csfavicon.ico
bingDailyPhoto.py 为上面写的代码文件名称
csfavicon.ico 为刚才制作的ico,如果不是在本目录下请写绝对路径
5).打包后目录结构
pyinstaller 此时会生成相应的 spec 文件,大体流程如下:
1、在脚本目录生成 xxx.spec 文件 (取决于 -n 参数,没传,则与 xxx.py 同名为 xxx);
2、创建一个 build 目录;
3、写入一些日志文件和中间流程文件到 build 目录;
4、创建 dist 目录;
5、生成可执行文件或文件夹到 dist 目录;
6).点击运行
点击运行bingDailyPhoto.exe文件即可运行获取Bing图片了
7).设置开机自启
设置为开机自启,每天自动获取新的Bing图片设置为桌面壁纸,美滋滋呀
方法可以参考百度经验设置:Win10怎么设置开机自启动软件 如何打开指定应用