聚合api接口查询电话的运营商

前端之家收集整理的这篇文章主要介绍了聚合api接口查询电话的运营商前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
__author__='jk409'
#python3.4
#使用聚合api接口
#根据手机号查询isp运营商,并更新到表里
importsys,time,urllib.request,urllib.parse
sys.path.append('./')
importsql
importjson
#------------------------------------------------------
defMysqLs():
returnsql.MysqL('127.0.0.1','root','123456','msc')
#------------------------------------------------------

defrequest1(phone,appkey,m="GET"):
url="http://apis.juhe.cn/mobile/get"
params={
"phone":phone,#需要查询的手机号码或手机号码前7位
"key":appkey,#应用APPKEY(应用详细页查询)
"dtype":"json",#返回数据的格式,xml或json,默认json
}
params=urllib.parse.urlencode(params)
ifm=="GET":
f=urllib.request.urlopen("%s?%s"%(url,params))
else:
f=urllib.request.urlopen(url,params)
content=f.read().decode('utf-8')
res=json.loads(content)
ifres:
error_code=res["error_code"]
iferror_code==0:
#成功请求
#print(res["result"])
returnres["result"]["company"]
else:
print("%s:%s"%(res["error_code"],res["reason"]))
else:
print("requestapierror")

defrun():
MysqL=MysqLs()
sqls="selectuid,phonefrommsc_user_defwhereispisnullorderbyuidlimit10"
try:
res=MysqL.cmd(sqls)
except:
pass
MysqL.close()
res_list=[list(x)forxinres]
#foriinres_list:
#print(i[0])
returnres_list

if__name__=="__main__":
dc={}
appkey='a28516a0d19a*************'#需要正确填写你的appkey
phone_list=run()
print('请稍后,正常查询......')
foriinphone_list:
isp=request1(i[1],'GET')
#print(i[0],i[1],isp)
dc[i[0]]={}
dc[i[0]]['phone']=str(i[1])
dc[i[0]]['isp']=isp

#print(dc)
#isp_type:1移动,2电信,3联通
foriindc:
isp_type=0
MysqL=MysqLs()
ifdc[i]['isp']=="中国移动":isp_type=1
ifdc[i]['isp']=='中国电信':isp_type=2
ifdc[i]['isp']=='中国联通':isp_type=3
print(dc[i]['phone'],dc[i]['isp'])
print(isp_type)
sqls="updatemsc_user_defsetisp='%s',isp_type='%s'wherephone='%s'"
try:
MysqL.cmd(sqls%(dc[i]['isp'],isp_type,dc[i]['phone']))
#MysqL.commit()
except:
print('phone',dc[i]['phone'])
pass
MysqL.close()
pass
原文链接:https://www.f2er.com/javaschema/284016.html

猜你在找的设计模式相关文章