Python獲取中國節(jié)假日數(shù)據(jù)記錄入JSON文件
項目系統(tǒng)內(nèi)置的日歷應(yīng)用為了提升用戶體驗,特別設(shè)置了在調(diào)休日期顯示“休”的UI圖標(biāo)功能。那么問題是這些調(diào)休數(shù)據(jù)從哪里來呢?
開發(fā)盆友首先訪問政府官網(wǎng),查閱并記錄下年度的節(jié)假日及調(diào)休安排,再錄入數(shù)據(jù)庫。作為追求效率與自動化的我(懶),并不認可這種“可愛”的方式。
我嘗試一種更為智能的方法:Python獲取中國節(jié)假日數(shù)據(jù)記錄入JSON文件。
節(jié)假日數(shù)據(jù)獲取
獲取地址: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' # 網(wǎng)址
res = requests.get(url=url, timeout=10) # 發(fā)送請求
print(res.json())運行結(jié)果:

存入JSON文件
tinydb創(chuàng)建JSON文件,插入獲取到的數(shù)據(jù)
from tinydb import TinyDB
if res.status_code == 200: # 校驗是否返回數(shù)據(jù)
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ù)據(jù)
db.insert({'year': y, 'days': d, 'papers': p}) # 插入數(shù)據(jù)運行結(jié)果:

節(jié)假日數(shù)據(jù)讀取
保存的節(jié)假日數(shù)據(jù)是以年份為名稱的不同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__)))] # 遍歷當(dāng)前文件夾
json_file_list = [os.path.splitext(f)[0] for f in files[0]] # 分割文件名
if str(year) in json_file_list: # 校驗是否存在年份數(shù)據(jù)
with TinyDB(f"{year}.json") as db: # 打開tinydb
print(db.all()) # 獲取所有數(shù)據(jù)
else:
print(f'{year}年數(shù)據(jù)不存在')運行結(jié)果:

封裝完整代碼
import os
import traceback
import requests
from tinydb import TinyDB
class ChineseHoliday:
"""
中國節(jié)假日獲取
"""
@staticmethod
def download(year):
"""
獲取并保存節(jié)假日json數(shù)據(jù)
獲取地址來源:https://github.com/NateScarlet/holiday-cn
:return:
"""
try:
url = f'https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/{year}.json' # 網(wǎng)址
res = requests.get(url=url, timeout=10) # 發(fā)送請求
# print(res.json())
if res.status_code == 200: # 校驗是否返回數(shù)據(jù)
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ù)據(jù)
db.insert({'year': y, 'days': d, 'papers': p}) # 插入數(shù)據(jù)
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__)))] # 遍歷當(dāng)前文件夾
json_file_list = [os.path.splitext(f)[0] for f in files[0]] # 分割文件名
if str(year) in json_file_list: # 校驗是否存在年份數(shù)據(jù)
with TinyDB(f"{year}.json") as db: # 打開tinydb
return db.all() # 獲取所有數(shù)據(jù)
return到此這篇關(guān)于Python獲取中國節(jié)假日數(shù)據(jù)記錄入JSON文件的文章就介紹到這了,更多相關(guān)Python獲取中國節(jié)假日數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中if __name__ == ''__main__''作用解析
這篇文章主要介紹了Python中if __name__ == '__main__'作用解析,這斷代碼在Python中非常常見,它有作用?本文就解析了它的作用,需要的朋友可以參考下2015-06-06
python?web?開發(fā)之Flask中間件與請求處理鉤子的最佳實踐
Flask作為輕量級Web框架,提供了靈活的請求處理機制,中間件和請求鉤子允許開發(fā)者在請求處理的不同階段插入自定義邏輯,實現(xiàn)諸如身份驗證、日志記錄、數(shù)據(jù)預(yù)處理等功能,本文將詳細介紹Flask的請求處理鉤子、g對象以及中間件模式的使用方法和最佳實踐,感興趣的朋友一起看看吧2025-05-05
Python實現(xiàn)TXT數(shù)據(jù)轉(zhuǎn)三維矩陣
在數(shù)據(jù)處理和分析中,將文本文件中的數(shù)據(jù)轉(zhuǎn)換為三維矩陣是一個常見的任務(wù),本文將詳細介紹如何使用Python實現(xiàn)這一任務(wù),感興趣的小伙伴可以了解下2024-01-01
PYTHON如何讀取和寫入EXCEL里面的數(shù)據(jù)
這篇文章主要介紹了PYTHON如何讀取和寫入EXCEL里面的數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
詳解pyppeteer(python版puppeteer)基本使用
這篇文章主要介紹了詳解pyppeteer(python版puppeteer)基本使用 ,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

