曾经写过一篇文章使用python查询天气,是调用中国天气网的接口实现的天气查询,下面还是用该接口实现一个本地网页的天气查询。如图:
服务端
代码:
const http = require("http");
const url = require("url");
const querystring = require("querystring");
const server = http.createServer((req, res) => {
let { query } = url.parse(req.url, true);
let cityCode = query.citycode;
let apiUrl = `http://www.weather.com.cn/data/sk/${cityCode}.html`;
http.get(apiUrl, function(res1) {
let jsonData = "";
res1.on("data", function(data) {
jsonData += data.toString("utf8");
});
res1.on("end", function() {
res.setHeader('Access-Control-Allow-Origin', '*');
res.write(jsonData);
res.end();
});
});
});
server.listen(8080);
网页端
代码:
<!DOCTYPE html>
<html>
<head>
<meta