#Date:2019.5.13
#author : isenwen
#describe : 历史上的今天爬虫

import requests
import json
import os,time

def get_news(params):
    url = 'http://api.juheapi.com/japi/toh'
    response = requests.get(url,params=params).text
    history_lists =json.loads(response)['result']
    return history_lists

def get_detail(history):
    year = history['year']
    title =history['title']
    return str(year)+'年'+title+'\n'

def save_data(res):
    exist =  os.path.exists('D://test//NEWS')
    if exist == False:
        os.makedirs('D://test//NEWS')
    else:
        f =open('D://test//NEWS//历史上的今天.txt','a')
        f.write(res)
        f.close()







if __name__ == '__main__':
    month = int(time.strftime('%m',time.localtime()))
    day = int(time.strftime('%d',time.localtime()))
    key = '213bbb4be9d0db406d402d19c06fdee0'
    params = 'v=&month={}&day={}&key={}'.format(month,day,key)
    history_lists = get_news(params)
    for history in history_lists:
        res = get_detail(history)
        save_data(res)

    print('文件已经保存至D盘test文件下')