注意:该接口和代码仅供学习使用!请勿将传回的壁纸用于非法用途!请尊重版权!树立版权意识!
由于读者自身行为导致的侵权,作者概不负责!
Bing壁纸
- Bing的壁纸是每天更换的,而且更换的壁纸都很漂亮,而如何把它变成自己的博客幕布呢?于是想到做一个Bing壁纸的接口。
- 注:如果你不想折腾,那你可以直接使用我提供的接口:https://bingapi.littlewith.tk/?id=1
- Bing在官方确实有给出具体的接口:网址为:http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
- 官方接口返回的是json数据:
{ "images": [ { "startdate": "20221031", "fullstartdate": "202210310700", "enddate": "20221101", "url": "/th?id=OHR.HohenzollernHechingen_ROW5987672369_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp", "urlbase": "/th?id=OHR.HohenzollernHechingen_ROW5987672369", "copyright": "Hohenzollern Castle, Swabian Alps, Baden-Württemberg, Germany (© Cornelia Dorr/eStock Photo)", "copyrightlink": "https://www.bing.com/search?q=hohenzollern+castle+wiki&form=hpcapt", "title": "Info", "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20221031_HohenzollernHechingen%22&FORM=HPQUIZ", "wp": true, "hsh": "99656c4ae8b11500b6806b76d858a527", "drk": 1, "top": 1, "bot": 1, "hs": [] } ], "tooltips": { "loading": "正在加载...", "previous": "上一个图像", "next": "下一个图像", "walle": "此图片不能下载用作壁纸。", "walls": "下载今日美图。仅限用作桌面壁纸。" } }
- 接口中包含着今日壁纸的信息
"url": "/th?id=OHR.HohenzollernHechingen_ROW5987672369_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp"
制作的Python程序来获取美图
原理:使用requests模块获取数据后,进行字符串的拼接形成可访问的最终图片网址,再次通过requests请求图片缓存,最后通过flask传回获取到的数据。
最终实现的源码如下:
第1个文件
#!/usr/bin/getpaper.py import requests import time import json import wget def get_json(url): headers = { 'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" } resp = requests.get(url=url,headers=headers) jsoned = json.loads(resp.content.decode("utf-8")) finalu = jsoned['images'][0]['url'] return "https://www.bing.com" + finalu pass def get_dir(content): filename = wget.download(url=content, out='./' + str(int((time.time()))) + '.jpg') #filenamed = filename.split("/") return filename pass def get_data(url): headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" } respond = requests.get(url=url, headers=headers) return respond.content pass def done(): content = get_json("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1") #result = get_dir(content) # result里面为文件路径以及文件名字 result = get_data(content) return result pass
第2个文件
#!/usr/bin/webmain.py from flask import * import flask import getpaper import os app = flask.Flask(__name__) @app.route('/') def back(): filename = getpaper.done() #image = open(filename, "rb+") resp = Response(filename, mimetype="image/jpg") return resp if __name__=="__main__": app.run(host="0.0.0.0", port=4321, debug=True)
请将两个文件放在同一个目录,运行webmain.py中的内容
请勿将本程序运用于非法用途!