Python獲取中國節(jié)假日數(shù)據記錄入JSON文件
項目系統(tǒng)內置的日歷應用為了提升用戶體驗,特別設置了在調休日期顯示“休”的UI圖標功能。那么問題是這些調休數(shù)據從哪里來呢?
開發(fā)盆友首先訪問政府官網,查閱并記錄下年度的節(jié)假日及調休安排,再錄入數(shù)據庫。作為追求效率與自動化的我(懶),并不認可這種“可愛”的方式。
我嘗試一種更為智能的方法:Python獲取中國節(jié)假日數(shù)據記錄入JSON文件。
節(jié)假日數(shù)據獲取
獲取地址:https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/年份.json
requests請求即可
import requests year = 2024 url = f'https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/{year}.json' # 網址 res = requests.get(url=url, timeout=10) # 發(fā)送請求 print(res.json())
運行結果:
存入JSON文件
tinydb創(chuàng)建JSON文件,插入獲取到的數(shù)據
from tinydb import TinyDB if res.status_code == 200: # 校驗是否返回數(shù)據 res_data = res.json() y = res_data.get('year') d = res_data.get('days') p = res_data.get('papers') with TinyDB(f'{year}.json') as db: # 創(chuàng)建/打開tinydb db.truncate() # 清空數(shù)據 db.insert({'year': y, 'days': d, 'papers': p}) # 插入數(shù)據
運行結果:
節(jié)假日數(shù)據讀取
保存的節(jié)假日數(shù)據是以年份為名稱的不同JSON文件,使用tinydb讀取即可
import os from tinydb import TinyDB year = 2022 files = [files for root, dirs, files in os.walk(os.path.dirname(os.path.abspath(__file__)))] # 遍歷當前文件夾 json_file_list = [os.path.splitext(f)[0] for f in files[0]] # 分割文件名 if str(year) in json_file_list: # 校驗是否存在年份數(shù)據 with TinyDB(f"{year}.json") as db: # 打開tinydb print(db.all()) # 獲取所有數(shù)據 else: print(f'{year}年數(shù)據不存在')
運行結果:
封裝完整代碼
import os import traceback import requests from tinydb import TinyDB class ChineseHoliday: """ 中國節(jié)假日獲取 """ @staticmethod def download(year): """ 獲取并保存節(jié)假日json數(shù)據 獲取地址來源:https://github.com/NateScarlet/holiday-cn :return: """ try: url = f'https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/{year}.json' # 網址 res = requests.get(url=url, timeout=10) # 發(fā)送請求 # print(res.json()) if res.status_code == 200: # 校驗是否返回數(shù)據 y = res.json().get('year') d = res.json().get('days') p = res.json().get('papers') with TinyDB(f'{year}.json') as db: # 創(chuàng)建/打開tinydb db.truncate() # 清空數(shù)據 db.insert({'year': y, 'days': d, 'papers': p}) # 插入數(shù)據 except Exception as e: info = f"出了點小問題!\n{repr(e)}\n{traceback.format_exc()}" print(info) @staticmethod def get(year): files = [files for root, dirs, files in os.walk(os.path.dirname(os.path.abspath(__file__)))] # 遍歷當前文件夾 json_file_list = [os.path.splitext(f)[0] for f in files[0]] # 分割文件名 if str(year) in json_file_list: # 校驗是否存在年份數(shù)據 with TinyDB(f"{year}.json") as db: # 打開tinydb return db.all() # 獲取所有數(shù)據 return
到此這篇關于Python獲取中國節(jié)假日數(shù)據記錄入JSON文件的文章就介紹到這了,更多相關Python獲取中國節(jié)假日數(shù)據內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Python實現(xiàn)為PDF文檔設置背景色或背景圖
PDF作為一種跨平臺、高保真的文件格式被廣泛應用,這篇文章主要為大家詳細介紹了如何使用Python代碼對PDF文檔進行頁面背景色或背景圖片的設置,需要的可以參考下2024-04-04Python使用flask作為web服務器的代碼實現(xiàn)
Python Flask 框架是一個輕量級的 Web 框架,它簡單易用,靈活多變,非常適合用于構建小型到中型規(guī)模的 Web 應用程序,本文給大家介紹了Python使用flask作為web服務器的代碼實現(xiàn),需要的朋友可以參考下2024-06-06Python?Requests?基本使用及Requests與?urllib?區(qū)別
在使用Python爬蟲時,需要模擬發(fā)起網絡請求,主要用到的庫有requests庫和python內置的urllib庫,一般建議使用requests,它是對urllib的再次封裝,今天通過本文給大家講解Python?Requests使用及urllib區(qū)別,感興趣的朋友一起看看吧2022-11-11selenium+python實現(xiàn)自動化登錄的方法
這篇文章主要介紹了selenium+python實現(xiàn)自動化登錄的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09