平常生活中有一些常見的疾病,這個能夠經過百度等搜索到,可是若是你要完成一款app或者小程序、網站之類的該如何來獲取常見疾病的信息呢?首先想到的是經過爬蟲爬取數據,而後整理搜索....其實這種方法仍是太曲折了,網上有不少免費的api接口,如今已nodejs爲例來看如何獲取數據:node
var http = require('http'); var qs = require('querystring'); //配置您申請的appKey和openId app_key = "***"; open_id = "***"; function request_content(request_url,port,params,method){ var path=request_url; if(!!params){ var content = qs.stringify(params); path=request_url+'?' + content; } var options = { port: port, path: path, method: method }; if(method.toLowerCase()=='post'){ options['headers']="Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8"; } var req = http.request(options, function (res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log(chunk); }); }); req.on('error', function (e) { console.log('problem with request: ' + e.message); }); req.end(); } function main(){ var domain="http://api.xiaocongjisuan.com/"; var port=8080;//http對應80端口,https 對應443端口,請使用者本身改正 var servlet="life/commondisease/get"; var method="post"; var request_url=domain+servlet; var params = {}; params['appKey']=app_key; params['openId']=open_id; //變更部分 params["keyword"]="神經外科"; params["field"]="department"; params["highlight"]=1; params["pageSize"]=10; params["currentPage"]=1; request_content(request_url,port,params,method); } main();
再以python爲例,其實也很是容易實現:python
# -*- coding: utf-8 -*- # flake8: noqa __author__ = 'wukong' import urllib from urllib import urlencode #配置您申請的appKey和openId app_key="***" open_id="***" """ request_url 請求地址 params 請求參數 method 請求方法 """ def request_content(request_url,params,method): params = urlencode(params) if method and method.lower() =="get": f = urllib.urlopen("%s?%s" % (request_url, params)) else: f = urllib.urlopen(request_url, params) content = f.read() print content def main(): domain="http://api.xiaocongjisuan.com/"; servlet="life/commondisease/get" method="post" request_url=domain+servlet #字典 params ={} params["docName"]=app_key params["openId"]=open_id #變更部分 params["keyword"]="神經外科" params["field"]="department" params["highlight"]=1 params["pageSize"]=10 params["currentPage"]=1 request_content(request_url,params,method) if __name__ == '__main__': main()