日常生活中有一些常见病,可以通过百度等搜索,但是如果要完成一个APP、小程序、网站等,如何获取常见病的信息呢?最先想到的就是通过爬虫爬取数据,然后整理搜索....其实这个方法还是太曲折了。网上有很多免费的API接口。现在以nodejs为例,看看如何获??取数据:varhttp=require('http');varqs=require('querystring');//配置你申请的appKey和openIdapp_key="***";open_id="***";functionrequest_content(request_url,port,params,method){varpath=request_url;if(!!params){varcontent=qs.stringify(params);路径=request_url+'?'+内容;}varoptions={port:端口,path:路径,method:方法};if(method.toLowerCase()=='post'){options['headers']="Content-Type':'application/x-www-form-urlencoded;charset=UTF-8";}varreq=http.request(options,function(res){res.setEncoding('utf8');res.on('data',function(chunk){console.log(chunk);});});req.on('error',function(e){console.log('problemwithrequest:'+e.message);});req.end();}functionmain(){vardomain="http://api.xiaocongjisuan.com/";varport=8080;//http对应80端口,https对应443端口,请自行修正varservlet="life/commondisease/get";变种方法=“发布”;varrequest_url=域+servlet;变量参数={};参数[‘appKey’]=app_key;参数['openId']=open_id;//更改部分参数[“关键字”]=“神经外科”;参数[“字段”]=“部门”;参数[“突出显示”]=1;参数["pageSize"]=10;参数[“当前页面”]=1;request_content(request_url,port,params,method);}main();以python为例,其实很容易实现:#-*-coding:utf-8-*-#flake8:noqa__author__='wukong'importurllibfromurllibimporturlencode#ConfigureyourapplicationappKeyandopenId,method):params=urlencode(params)ifmethod.lower()=="get":f=urllib.urlopen("%s?%s"%(request_url,params))else:f=urllib.urlopen(request_url,params)content=f.read()打印内容defmain():domain="http://api.xiaocongjisuan.com/";servlet="life/commondisease/get"method="post"request_url=domain+servlet#字典params={}params["docName"]=app_keyparams["openId"]=open_id#变量部分params["keyword"]="神经外科"params["field"]="department"params["highlight"]=1params["pageSize"]=10params["currentPage"]=1request_content(request_url,params,method)if__name__=='__main__':主要()
