Last active
January 19, 2017 12:49
-
-
Save ddrpa/8d7a7b27e1edf83e2e17d61392d75a79 to your computer and use it in GitHub Desktop.
获取必应中国今日美图的 python 脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Update 2017.1.15
发现了 Bing 壁纸的 API
下一步要通过文件体积判断是否是无效图片。
以及 API 是可以一次返回数天图片地址的,最好写成参数。
Update 2017.1.19
可能由于 crontab
的执行机制,文件路径为 ./
时会指向用户的 home
,修改为绝对路径解决问题。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update 2016.10.20
因为脚本写在虚拟机里,防止没有开机导致
crontab
执行失败,增加执行次数,并使用os.path
检验文件是否存在