Python学习笔记:必应壁纸下载

发布时间 2023-11-09 09:43:42作者: Hider1214

GitHub 上开源的必应壁纸 API 作为数据来源。

-- 1.开源地址
https://github.com/zenghongtu/bing-wallpaper

-- 2.请求接口
https://bingw.jasonzeng.dev/

一、接口使用说明

1.API

接口:https://bingw.jasonzeng.dev

2.分辨率 resolution

默认分辨率为:1920×1080,其他可设置参数为:

UHD      # 高清 图片质量好
1920x1200
1920x1080
1366x768
1280x768
1024x768
800x600
800x480
768x1280
720x1280
640x480
480x800
400x240
320x240
240x320

3.返回格式 format

返回的格式默认为 json,未设置的情况下,会直接重定向为壁纸图片。

4.指定天数(索引) index

索引默认从 0 开始,获取今天的图像。1 获取昨天的,以此类推。

负数则反序,-1 获取最近一天图像。

random 选择随机一天。

5.日期 date

使用日期进行获取,从 20190309 至今,按此格式。

6.图像宽度 w

7.图像高度 h

8.图像质量 qlt

取值为 0-100

二、实操

1.请求

http://bingw.jasonzeng.dev?resolution=UHD&index=random&w=1000&format=json

2.返回

{
    "startdate":"20190814",
    "copyright":"Male and female Ecuadorian horned anoles in Mindo, Ecuador (© James Christensen/Minden Pictures)",
    "urlbase":"/th?id=OHR.HornedAnole_EN-US5022096617",
    "title":"Lizard of mystery",
    "url":"https://www.bing.com/th?id=OHR.HornedAnole_EN-US5022096617_UHD.jpg&w=1000"
}

三、Python调用

1.简单例子

import requests
import json
import os
os.chdir(r'C:\Users\111\Desktop\wallpaper')

url = "http://bingw.jasonzeng.dev?resolution=UHD&index=random&w=1000&format=json"
print(url)
res = requests.get(url)
url2 = json.loads(res.text)['url']
print(url2)
res2 = requests.get(url2)

with open('test.jpg', 'wb') as w:
    w.write(res2.content)

2.循环遍历

# -*- coding: utf-8 -*-
"""
Created on Thu Nov  9 09:23:20 2023

@author: Hider
"""
import requests
import json
import os
os.chdir(r'C:\Users\111\Desktop\wallpaper')
import time

def get_wallpaper():
    for i in range(100):
        print(f'-------- {i} --------')
        url = f'http://bingw.jasonzeng.dev?resolution=UHD&index={i}&w=1920&h=1080&format=json&qlt=100'
        print(url)
        res = requests.get(url)
        url2 = json.loads(res.text)['url']
        print(url2)
        res2 = requests.get(url2)
        with open(f'image/{i}.jpg', 'wb') as w:
            w.write(res2.content)
        time.sleep(5)

if __name__ == "__main__":
    get_wallpaper()

3.结果展示

参考链接:bing-wallpaper

参考链接:用Python下载壁纸并自动更换桌面