Skip to content

Instantly share code, notes, and snippets.

@ddrpa
Last active January 19, 2017 12:49
Show Gist options
  • Save ddrpa/8d7a7b27e1edf83e2e17d61392d75a79 to your computer and use it in GitHub Desktop.
Save ddrpa/8d7a7b27e1edf83e2e17d61392d75a79 to your computer and use it in GitHub Desktop.
获取必应中国今日美图的 python 脚本
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import codecs
import json
import re
import os.path
import urllib.request
import datetime
storageLocation = "C:\\Code\\python\\BingImages\\"
r = urllib.request.urlopen("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN")
jsonData = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))
# with open('data.json', 'r', encoding="utf8") as bingAPI:
# jsonData = json.load(bingAPI)
imgURLTail = jsonData["images"][0]['url']
imgURlHead = 'http://www.bing.com'
imgURL = imgURlHead + imgURLTail
#print(imgURL)
regExRes = re.search(r'[a-z|A-Z]+(?=\_)',imgURLTail)
imgName = regExRes.group(0)
if(os.path.isfile(storageLocation + imgName + ".jpg") == False):
# Download image
urllib.request.urlretrieve(imgURL, storageLocation + imgName + ".jpg")
# Write Logs
with open(storageLocation + "log.txt", "a") as f:
log = str(datetime.datetime.now().strftime("%Y%m%d") + " : " + imgName + "\r\n")
f.write(log)
@ddrpa
Copy link
Author

ddrpa commented Jan 15, 2017

Update 2017.1.15

发现了 Bing 壁纸的 API

下一步要通过文件体积判断是否是无效图片。
以及 API 是可以一次返回数天图片地址的,最好写成参数。

@ddrpa
Copy link
Author

ddrpa commented Jan 19, 2017

Update 2017.1.19

可能由于 crontab 的执行机制,文件路径为 ./ 时会指向用户的 home ,修改为绝对路径解决问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment