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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update 2017.1.19
可能由于
crontab
的执行机制,文件路径为./
时会指向用户的home
,修改为绝对路径解决问题。