import requests

# api主要提供以下4个接口，其中1个用于调取天气预报数据，3个用于调取历史天气数据
# 1.调取某日天气预报数据，时间跨度：选定日期起未来10日，共240小时
# 2.调取某日历史天气数据，时间跨度：选定日期的当日，共24小时
# 3.调取某月历史天气数据，时间跨度：选定年月的当月，共30日720小时
# 4.调取某年历史天气数据，时间跨度：选定年的当年，共12月365日8760小时

# API Key可以在“个人中心”页面查看，所有用户都有API Key但是仅专业用户可以使用API功能，所有接口都需要API Key
api_key = "您的26位API Key"


#-------------------------------------1.调取某日天气预报数据----------------------------------------
# 日期年月日参数，整型，须为近30日的日期
year, month, day = 2025, 9, 26 

# 地名参数，目前收录全国370个主要城市，城市列表请在说明文档中下载查看（https://app.cornicelli.net/meteo/download/resource/cities_list.csv）
city_name = '西安市'

# 构造url，获取该城市该日期起，未来10日逐小时天气预报
forecast_daily_url = f"https://app.cornicelli.net/meteo/api/forecast/daily/{city_name}/{year}/{month}/{day}/{api_key}"
forecast_daily_data = requests.get(forecast_daily_url)
print(forecast_daily_data.text)

#-------------------------------------2.调取某日历史天气数据----------------------------------------
# 日期年月日参数，整型，须为2000年1月1日至今日之间的日期，历史天气数据更新有1个月左右的延迟，所以近1个月的数据可能无法调取
year, month, day = 2025, 3, 26 

# 地名参数，目前收录全国370个主要城市，城市列表请在说明文档中下载查看（https://app.cornicelli.net/meteo/download/resource_cities_list.csv）
city_name = '南京市'

# 构造url，获取该城市该日期内24个时次的历史天气数据
history_daily_url = f"https://app.cornicelli.net/meteo/api/history/daily/{city_name}/{year}/{month}/{day}/{api_key}"
history_daily_data = requests.get(history_daily_url)
print(history_daily_data.text)

#-------------------------------------3.调取某月历史天气数据----------------------------------------
# 年月参数，整型，须为2000年1月至今日之间的月份，历史天气数据更新有1个月左右的延迟，所以近1个月的数据可能无法调取
year, month = 2025, 3

# 地名参数，目前收录全国370个主要城市，城市列表请在说明文档中下载查看（https://app.cornicelli.net/meteo/download/resource/cities_list.csv）
city_name = '成都市'

# 构造url，获取该城市该月份内720个时次的历史天气数据
history_monthly_url = f"https://app.cornicelli.net/meteo/api/history/monthly/{city_name}/{year}/{month}/{api_key}"
history_monthly_data = requests.get(history_monthly_url)
print(history_monthly_data.text)


#-------------------------------------4.调取某年历史天气数据----------------------------------------
# 年份参数，整型，须为2000年至今日之间的年份，历史天气数据更新有1个月左右的延迟，所以近1个月的数据可能无法调取
year = 2025

# 地名参数，目前收录全国370个主要城市，城市列表请在说明文档中下载查看（https://app.cornicelli.net/meteo/download/resource/cities_list.csv）
city_name = '上海市'

# 构造url，获取该城市该年份内8760个时次的历史天气数据
history_yearly_url = f"https://app.cornicelli.net/meteo/api/history/yearly/{city_name}/{year}/{api_key}"
history_yearly_data = requests.get(history_yearly_url)
print(history_yearly_data.text)

