Logo
Section 1 / 2

每日预报

学习如何获取特定位置的每日天气预报信息。

导入

每日预报

学习如何获取特定位置的每日天气预报信息。

1http://dataservice.accuweather.com/forecasts/v1/daily/1day/{locationKey}
1http://dataservice.accuweather.com/forecasts/v1/daily/5day/{locationKey}
参数类型分类描述
API_KEY字符串必需这是我们的API密钥。这是一个查询参数。
locationKey整数必需这是位置的键。这是一个路径参数。
language字符串可选返回结果的语言。默认值为 en-us。这是一个查询参数。
details布尔值可选设置为 true 以在响应中包含完整详情,否则设置为 false。默认值为 false。这是一个查询参数。
metric布尔值可选设置为 true 以在响应中显示公制单位的数值,否则设置为 false。默认值为 false。这是一个查询参数。
1// 在此处导入库 2import fetch from "node-fetch"; 3 4// 在此处定义API密钥 5const API_KEY = '{{API_KEY}}'; 6 7// 在此处定义路径参数 8const locationKey = 351409; 9 10// 在此处定义端点URL 11const url1Day = new URL(`http://dataservice.accuweather.com/forecasts/v1/daily/1day/${locationKey.toString()}`); 12const url5Day = new URL(`http://dataservice.accuweather.com/forecasts/v1/daily/5day/${locationKey.toString()}`); 13 14// 在此处定义头参数 15const headerParameters = { 16 contentType: 'application/json', 17}; 18 19// 在此处定义查询参数 20const queryParams1Day = new URLSearchParams({ 21 apikey: API_KEY, 22}); 23 24const queryParams5Day = new URLSearchParams({ 25 apikey: API_KEY, 26}); 27 28// 设置API调用选项 29const options = { 30 method: 'GET', 31 headers: headerParameters, 32}; 33 34// 定义发起API调用的函数 35async function getForecasts(endpointUrl, queryParameters) { 36 try { 37 endpointUrl.search = queryParameters; 38 const response = await fetch(endpointUrl, options); 39 40 // 自定义函数:打印API响应 41 printResponse(response); 42 } catch (error) { 43 // 自定义函数:打印错误信息 44 printError(error); 45 } 46} 47 48async function getDailyForecasts() { 49 await getForecasts(url1Day, queryParams1Day); 50 await getForecasts(url5Day, queryParams5Day); 51} 52 53// 调用函数以发起API调用 54getDailyForecasts();
参数类型描述
Headline对象这是在指定时间段内的最重要的天气描述。
DailyForecasts.Day对象指的是白天的天气预报信息。
DailyForecasts.Night对象指的是夜间的天气预报信息。
DailyForecasts.Temperature.Maximum对象表示最高温度。
DailyForecasts.Temperature.Minimum对象表示最低温度。
DailyForecasts.Link字符串AccuWeather网站上详细预报的链接。
DailyForecasts.MobileLink字符串AccuWeather移动网站上详细预报的链接。

位置 API

每小时预报