import requests
from datetime import date
# 调用老黄历 API 查询指定日期的宜忌
api_url = "https://api.example.com/huangli"
query_date = date(2024, 12, 21) # 冬至
params = {
"date": query_date.isoformat(),
"start_year": 1900,
"end_year": 2100
}
try:
resp = requests.get(api_url, params=params, timeout=10)
resp.raise_for_status()
data = resp.json()
# 输出宜忌摘要
print(f"日期: {data['date']}")
print(f"宜: {', '.join(data['yi'])}")
print(f"忌: {', '.join(data['ji'])}")
print(f"吉时: {data['auspicious_hours']}")
print(f"冲煞: {data['conflict']}")
print(f"胎神: {data['fetal_god']}")
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
# 输出示例:
# 日期: 2024-12-21
# 宜: 祭祀, 祈福, 求嗣, 开光, 嫁娶
# 忌: 出行, 安葬, 作灶
# 吉时: 子时(23:00-01:00), 寅时(03:00-05:00)
# 冲煞: 冲蛇(己巳) 煞西
# 胎神: 占门炉外西北
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type HuangLi struct {
Date string `json:"date"`
Yi []string `json:"yi"`
Ji []string `json:"ji"`
AuspiciousHours string `json:"auspicious_hours"`
Conflict string `json:"conflict"`
FetalGod string `json:"fetal_god"`
}
func main() {
// 查询 2024 年 12 月 21 日(冬至)的老黄历
queryDate := time.Date(2024, 12, 21, 0, 0, 0, 0, time.UTC)
url := fmt.Sprintf("https://api.example.com/huangli?date=%s&start_year=1900&end_year=2100",
queryDate.Format("2006-01-02"))
resp, err := http.Get(url)
if err != nil {
fmt.Printf("请求失败: %v\n", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读取响应失败: %v\n", err)
return
}
var data HuangLi
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("解析 JSON 失败: %v\n", err)
return
}
fmt.Printf("日期: %s\n", data.Date)
fmt.Printf("宜: %v\n", data.Yi)
fmt.Printf("忌: %v\n", data.Ji)
fmt.Printf("吉时: %s\n", data.AuspiciousHours)
fmt.Printf("冲煞: %s\n", data.Conflict)
fmt.Printf("胎神: %s\n", data.FetalGod)
}
// 输出示例:
// 日期: 2024-12-21
// 宜: [祭祀 祈福 求嗣 开光 嫁娶]
// 忌: [出行 安葬 作灶]
// 吉时: 子时(23:00-01:00), 寅时(03:00-05:00)
// 冲煞: 冲蛇(己巳) 煞西
// 胎神: 占门炉外西北
// 使用 fetch 调用老黄历 API(浏览器或 Node.js 18+)
const queryDate = '2024-12-21'; // 冬至
async function getHuangLi(date) {
const url = `https://api.example.com/huangli?date=${date}&start_year=1900&end_year=2100`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(`日期: ${data.date}`);
console.log(`宜: ${data.yi.join(', ')}`);
console.log(`忌: ${data.ji.join(', ')}`);
console.log(`吉时: ${data.auspicious_hours}`);
console.log(`冲煞: ${data.conflict}`);
console.log(`胎神: ${data.fetal_god}`);
return data;
} catch (error) {
console.error('请求失败:', error);
}
}
// 执行查询
getHuangLi(queryDate);
// 输出示例:
// 日期: 2024-12-21
// 宜: 祭祀, 祈福, 求嗣, 开光, 嫁娶
// 忌: 出行, 安葬, 作灶
// 吉时: 子时(23:00-01:00), 寅时(03:00-05:00)
// 冲煞: 冲蛇(己巳) 煞西
// 胎神: 占门炉外西北