Python - 定时自动获取 Bing 首页壁纸

简介: Bing 首页的壁纸好看且每日更新,下面介绍如何使用 python 每日自动获取壁纸并保存。

一.引言

Bing 首页的壁纸好看且每日更新,下面介绍如何使用 python 每日自动获取壁纸并保存。

image.gif编辑

二.手动获取

自动获取前先介绍下如何手动获取,主要是了解壁纸的网页形式。

1.打开开发者模式

可以直接 F12 快捷键进入开发模式,在右侧栏中找到 s.cn.bing.net 选项

image.gif编辑

2.打开新的 Tab

双击对应位置选择 open in new Tab

image.gif编辑

即可得到完整壁纸,右键选择另存为即可保存至指定位置:

image.gif编辑

Tips:

这里获取的网址连接为:

https://s.cn.bing.net/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg

image.gif

其中 https://s.cn.bing.net 为前缀,/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg 为图像后缀,二者拼接即可得到壁纸地址,后续通过 python 爬虫也是基于该地址获取壁纸。

三.自动获取

1.官方API

Bing 官方提供 API 获取线上网页壁纸:官方API

https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN

image.gif

主要有 format、idx、n、mkt 四个参数:

参数 含义
format 返回数据形式 js - json xml - xml
idx 截止天数 0-今天 -1 - 截止至明天 1 截止至昨天
n 返回数量        
mkt 地区 zh-CN - 国区

测试过程中发现 n 的数量总是返回1。

2.Postman 调用接口

使用 Postman Get Api 查看下接口返回 json 的大致形式,没有 postman 也不影响后续获取壁纸,获取壁纸只需要 python 即可。

image.gif编辑

可以看到当前壁纸的详细信息,壁纸对应的地址是:"落基山国家公园的朗斯峰,科罗拉多州 (© Andrew R. Slaton/Tandem Stills + Motion),非常的漂亮。其 images 数组内还包含 url ,该 url 形式为:

"url": "/th?id=OHR.LongsPeak_ZH-CN5927119555_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp"

image.gif

与我们刚才手动寻找打开的 tab 地址只差前缀 https://s.cn.bing.net

https://s.cn.bing.net/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg

image.gif

所以 python 的执行逻辑比较清晰:

A.调用 API 获取 Json

B.通过 Json 获取壁纸地址,拼接前缀得到最终壁纸地址

C.将对应 content 生成 jpg 保存至本机

3.Python 实现

#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
    "Connection": "close",
}
def dumpBingWallpaper():
    # 解析 URL
    n = 1
    idx = 1
    url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx={}&n={}".format(idx, n)
    res = requests.get(url, headers=headers)
    res.encoding = 'utf8'
    jsonData = json.loads(res.text)
    uri = jsonData['images'][0]['url']
    # 获取图像地址与信息
    img = requests.get("https://s.cn.bing.net/" + uri, headers=headers).content
    desc = str(jsonData['images'][0]['copyright']).split(",")[0]
    dt = jsonData['images'][0]['startdate']
    # 输出地址
    output = '/Users/xudong11/Desktop/{}.jpg'.format(desc + "_" + dt)
    out = open(output, 'wb')
    out.write(img)
    out.close()
if __name__ == "__main__":
    dumpBingWallpaper()

image.gif

通过 copyright 和 startdate 获取图像简介与日期作为输出图像的名称,运行后在指定位置获取目标壁纸:

image.gif编辑

Tips:

通过 chorm 开发者工具获取图像信息的方法需要引入开发者工具包,有兴趣的同学也可以实现:

from selenium import webdriver
options = webdriver.ChromeOptions()
Chrome = webdriver.Chrome(options=options, desired_capabilities=capabilities)
wait = WebDriverWait(Chrome, 5)

image.gif

4.定时执行

A.定时脚本

首先添加定时 shell 脚本 run.sh,PWD 为 python 所在文件夹目录:

#!/bin/bash
path=${PWD}
cd $path
python DumpBingPic.py

image.gif

B.crontab 定时启动

20 11 * * * 代表每天 11:20 下载 Bing 图片:

20 11 * * * source ~/.bash_profile && cd ${PWD} && sh run.sh

image.gif

image.gif编辑

无需手动运行脚本,每天定时保存 bing 壁纸,非常的奈斯👍

四.Windows 聚焦

除了 Bing 壁纸,之前也整理过 windows 聚焦的开机壁纸获取方式,并最终生成 .exe 的可执行文件,windows 的同学有兴趣也可以继续参考:Python 提取Windos聚焦的登陆图片,运行 exe 即可获取最近 windows 聚焦的开机壁纸:

image.gif编辑

目录
相关文章
|
6月前
|
监控 安全 机器人
通过GitHub Actions给微信公众测试号和钉钉群定时推送消息(Python)
通过GitHub Actions给微信公众测试号和钉钉群定时推送消息(Python)
94 0
|
1月前
|
API Python
做一个合格的男友,用python制作每天定时给女朋友发送邮箱问候
做一个合格的男友,用python制作每天定时给女朋友发送邮箱问候
|
4月前
|
Python
Python 采集某网站的壁纸
Python 采集某网站的壁纸
14 0
|
5月前
|
编解码 前端开发 API
用Python下载壁纸并自动更换桌面
用Python下载壁纸并自动更换桌面
|
8月前
|
Linux Shell Python
crontab定时执行python脚本不成功解决方案
crontab定时执行python脚本不成功解决方案
|
8月前
|
Python
用Python实现定时自动化收取蚂蚁森林能量,再也不用担心忘记收取了
用Python实现定时自动化收取蚂蚁森林能量,再也不用担心忘记收取了
279 2
用Python实现定时自动化收取蚂蚁森林能量,再也不用担心忘记收取了
|
9月前
|
Python
女友让我每天半夜十二点给她发晚安?我用 Python 做了个定时发消息神器!怕她干嘛!
女友让我每天半夜十二点给她发晚安?我用 Python 做了个定时发消息神器!怕她干嘛!
|
9月前
|
数据采集 Python
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳
|
Python
使用Python自动发送邮件
使用Python自动发送邮件
298 0
使用Python自动发送邮件
|
12月前
|
jenkins 测试技术 持续交付
【实测】python模拟jenkins的定时设置时间库: 【 python_jenkins_monitor 】
【实测】python模拟jenkins的定时设置时间库: 【 python_jenkins_monitor 】