nodejs获取常见疾病数据示例

日常生活中有一些常见的疾病,这个可以通过百度等搜索到,但是如果你要完成一款app或者小程序、网站之类的该如何来获取常见疾病的信息呢?首先想到的是通过爬虫爬取数据,然后整理搜索....其实这种方法还是太曲折了,网上有很多免费的api接口,现在已nodejs为例来看如何获取数据:

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为例,其实也非常容易实现:

# -*- 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()

你可能感兴趣的:(nodejs获取常见疾病数据示例)