获取天气

当前天气

<script>
	// 获取天气的api接口网址
	var apiUrl = "https://geoapi.qweather.com/v2/city/lookup?location=fuling&adm=chongqing&key=1271def7ae8e49008a6f794eb30f2d87";

	// 发送请求获取天气信息
	fetch(apiUrl)
		.then(response => response.json())
		.then(data => {
			// 获取城市代码
			var cityCode = data.location[0].id;

			// 构建天气api接口网址
			var weatherApiUrl = "https://devapi.qweather.com/v7/weather/now?location=" + cityCode + "&key=1271def7ae8e49008a6f794eb30f2d87";

			// 发送请求获取天气信息
			fetch(weatherApiUrl)
				.then(response => response.json())
				.then(data => {
					// 获取天气和温度
					var weather = data.now.text;
					var temperature = data.now.temp;

					// 显示天气和温度
					document.getElementById("weather").innerHTML = "天气:" + weather;
					document.getElementById("temperature").innerHTML = "温度:" + temperature + "℃";
					document.getElementById("location").innerHTML = "地址:" + data.location.name;
				})
				.catch(error => console.error(error));
		})
		.catch(error => console.error(error));
</script>

原文地址: https://www.cveoy.top/t/topic/ho5D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录