nodejs data acquisition examples of common diseases

Everyday there are some common diseases, such as this through Baidu search, but if you want to accomplish a small app or program, the information on how to obtain a common disease sites like it? First thought was crawling reptile data, then finish the search .... In fact, this method is still too tortuous, and there are many online free api interface is now nodejs an example to see how to get the data:

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();

Then to python, for example, it is also very easy to implement:

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

Guess you like

Origin www.cnblogs.com/huangxie/p/11665115.html