每天获取的结果我都放在这个网站上面了 [aru_41]
接口
- https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
脚本
- # -*- coding: utf-8 -*-
- import requests
- import json
- import urllib
- import os
- import ssl
- from qiniu import Auth, put_file, etag, urlsafe_base64_encode
- import qiniu.config
- import datetime
- # from insert_to import insert
- start_url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
- # context = ssl._create_unverified_context()
- ssl._create_default_https_context = ssl._create_unverified_context
- def get_url():
- response = requests.get(start_url)
- url = "https://www.bing.com" + response.json()['images'][0]['url']
- image_name = response.json()['images'][0]['copyright'].split('(')[0] + '.jpg'
- # return url, image_name
- time = datetime.datetime.now()
- file_path = './images/{}-{}-{}'.format(time.year, time.month, time.day)
- try:
- if not os.path.exists(file_path):
- print('文件夹', file_path, '不存在,重新建立')
- os.makedirs(file_path)
- filename = '{}/{}'.format(file_path, image_name)
- urllib.request.urlretrieve(url, filename)
- except IOError as e:
- print('文件操作失败', e)
- except Exception as e:
- print('错误 :', e)
完整的项目
开源在 github 上,并且,每天的图片备份也在 github 上
项目开源地址 图片备份地址项目通过 cronjob 开定时获取下载,并上传到七牛云,github 上 cronjob 没有写完整,一些 git 的提交等等你自己补充
本文作者为 olei,转载请注明。