本文采用的接口是聚合数据提供的
python2.7环境,3.x环境试了下好像不支持,获取位置信息为空
如下为运用代码:
1 #coding=utf-8 2 ''' 3 Created on 2019年9月18日 4 5 @author: yanerfree 6 7 python2.7 8 ''' 9 10 import json,urllib 11 from urllib import urlencode 12 13 #---------------------------------- 14 # 移动联通基站调用示例代码 - 聚合数据 15 # 在线接口文档:http://www.juhe.cn/docs/8 16 #---------------------------------- 17 18 def main(loc): 19 20 #配置您申请的APPKey---需要申请 21 appkey = "******" 22 #1.基站定位 23 getAddress(appkey,loc,"GET") 24 25 #基站定位 26 def getAddress(appkey, loc, m="GET"): 27 url = "http://v.juhe.cn/cell/query" 28 params = { 29 "mnc" : loc[1], #移动基站:0 联通基站:1 默认:0 30 "lac" : loc[2], #小区号 31 "ci" : loc[3], #基站号 32 "hex" : "10", #进制类型,16或10,默认:10 33 "dtype" : "json", #返回的数据格式:json/xml/jsonp 34 "callback" : "", #当选择jsonp格式时必须传递 35 "key" : appkey, #APPKEY 36 37 } 38 params = urlencode(params) 39 if m =="GET": 40 f = urllib.urlopen("%s?%s" % (url, params)) 41 else: 42 f = urllib.urlopen(url, params) 43 44 content = f.read() 45 print 'content:',content 46 if content["error_code"] == 0: 47 #if content["reason"] == u"查询成功" 48 print '查询成功' 49 result = content["result"] 50 return result["adress"] 51 52 ''' 53 res = json.loads(content) 54 print 'res:',res 55 if res: 56 error_code = res["error_code"] 57 if error_code == 0: 58 #成功请求 59 print res["result"] 60 else: 61 print "%s:%s" % (res["error_code"],res["reason"]) 62 else: 63 print "request api error" 64 ''' 65 66 67 68 if __name__ == '__main__': 69 70 loc_str = '0-460,0,6350,53394,30,16,0,10-460,0,6350,53394,30,16,0,10-460,0,6350,53394,30,16,0,10' 71 72 loc = loc_str.split('-')[1].split(',') 73 print loc 74 main(loc) 75
python2.7查询成功:
content: {"reason":"查询成功","result":{"mcc":"460","mnc":"0","lac":"6350","ci":"53394","lat":"31.046659471148","lon":"121.761917116247","radius":"431","address":"上海市浦东新区惠南镇良丰新村3号楼"},"error_code":0}
python3.7查询失败:
content: b'{"resultcode":"101","reason":"xe9x94x99xe8xafxafxe7x9ax84xe8xafxb7xe6xb1x82KEY","result":null,"error_code":10001}'