通过NodeJS中的http.get 和 http.request模块两种方法,调用天气api

1. http.get(options[, callback])

官方示例是这样的:

http.get('http://www.google.com/index.html', (res) => { console.log(`Got response: ${res.statusCode}`); // consume response body res.resume(); }).on('error', (e) => { console.log(`Got error: ${e.message}`); });

通过这段代码,可以将http://www.google.com/index.html 这个网页上的所有源代码拉取下来,所以可以使用此模块编写Node爬虫。

通过对此段代码的改编,得到以下调用中国天气api的代码:


var http = require('http');
http.get("http://mobile.weather.com.cn/data/forecast/101010100.html?_=你的密钥", function(res) {
    var size = 0;
    var chunks = [];
  res.on('data', function(chunk){
      size += chunk.length;
      chunks.push(chunk);
  });
  res.on('end', function(){
      var data = Buffer.concat(chunks, size);
      console.log(data.toString())
  });
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

2. http.request(options[, callback])

官方实例是这样的:

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); }); res.on('end', () => { console.log('No more data in response.') }) }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); // write data to request body req.write(postData); req.end();

通过对此段代码的改编,得到以下调用中国天气api的代码:

var http=require('http');

var options = {
  hostname: 'mobile.weather.com.cn',
  path: '/data/forecast/101010100.html?_=你的密钥',
};

var req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); }); res.on('end', () => { console.log('No more data in response.') }) }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); // write data to request body req.end();

返回数据类似如下:


{"c":{"c1":"101010100","c2":"beijing","c3":"北京","c4":"beijing","c5":"北京","c6":"beijing","c7":"北京","c8":"china","c9":"中国","c10":"1","c11":"010","c12":"100000","c13":"116.391","c14":"39.904","c15":"33","c16":"AZ9010","c17":"+8"},
"f":{"f1": [ {"fa":"01","fb":"03","fc":"10","fd":"5","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:21|17:40"}, {"fa":"07","fb":"07","fc":"19","fd":"12","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:22|17:38"}, {"fa":"02","fb":"00","fc":"15","fd":"5","fe":"8","ff":"8","fg":"3","fh":"1","fi":"06:23|17:37"}, {"fa":"00","fb":"00","fc":"16","fd":"4","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:24|17:35"}, {"fa":"00","fb":"00","fc":"18","fd":"7","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:25|17:34"}, {"fa":"00","fb":"01","fc":"18","fd":"8","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:26|17:32"}, {"fa":"01","fb":"01","fc":"16","fd":"6","fe":"0","ff":"0","fg":"0","fh":"0","fi":"06:27|17:31"}], "f0":"201310121100"}}

详细信息分析如下:


//格式说明
var format={fa:图片1,fb:图片2,fc:温度1,fd:温度2,fe:风向1,ff:风向2,fg:风力1,fh:风力2,fi:日出日落};  

//定义天气类型
var weatherArr={"00":"晴","01":"多云","02":"阴","03":"阵雨","04":"雷阵雨","05":"雷阵雨伴有冰雹","06":"雨夹雪","07":"小雨","08":"中雨","09":"大雨","10":"暴雨","11":"大暴雨","12":"特大暴雨","13":"阵雪","14":"小雪","15":"中雪","16":"大雪","17":"暴雪","18":"雾","19":"冻雨","20":"沙尘暴","21":"小到中雨","22":"中到大雨","23":"大到暴雨","24":"暴雨到大暴雨","25":"大暴雨到特大暴雨","26":"小到中雪","27":"中到大雪","28":"大到暴雪","29":"浮尘","30":"扬沙","31":"强沙尘暴","53":"霾","99":""}; 

//定义风向数组 
var fxArr={"0":"无持续风向","1":"东北风","2":"东风","3":"东南风","4":"南风","5":"西南风","6":"西风","7":"西北风","8":"北风","9":"旋转风"};   

//定义风力数组 
var flArr={"0":"微风","1":"3-4级","2":"4-5级","3":"5-6级","4":"6-7级","5":"7-8级","6":"8-9级","7":"9-10级","8":"10-11级","9":"11-12级"};  

其中,101010100 是北京的城市ID,更多城市ID可以在此查看。
你的密钥 是你注册了中国天气网后,自动分发的密钥,如果你不想用中国天气网,也可以换成其他的,例如和风天气、中国和世界天气预报。
原理都是差不多的,换个网址基本上就ok了。

如果你不想注册,那么可以使用这两个api http://www.weather.com.cn/data/sk/101010100.html、http://www.weather.com.cn/data/cityinfo/101010100.html
如果你不想使用中国天气官方api,那么也可以试试下面这两个 根据城市名查询、根据城市编号id查询、根据城市编号id五日xml格式,提醒较全面、根据城市名五日xml格式,提醒较全面

代码在这里

你可能感兴趣的:(api,nodejs)