获取当前天气和温度

当前天气

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

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

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

			// 发送请求获取天气信息
			fetch(weatherApiUrl)
				.then(response => response.json())
				.then(data => {
					// 获取天气和温度
					const weather = data.now.text;
					const 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>
获取当前天气和温度 - 使用QWeather API

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

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