基于Python的身份证号码归属地数据库调用代码实例
- 格式:doc
- 大小:105.50 KB
- 文档页数:3
身份证信息查询接口查询身份证具体信息身份证信息查询接口可以查询身份证上具体信息,包括地区、性别、出生日期等情况。
接口名称:身份证信息查询接口接口平台:聚合数据接口地址:/idcard/index支持格式:JSON/XML请求方式:get请求示例:/idcard/index?key=您申请的KEY&cardno=330326************请求参数:名称类型必填说明cardno string Y 身份证号码dtype string Y 返回数据格式:json或xml,默认jsonkey string Y 你申请的key调用样例及调试工具:API测试工具返回字段:名称类型说明error_code int 返回码reason string 返回说明data - 返回结果集area string 地区sex string 性别birthday string 出生日期身份证号码归属地数据库调用代码JSON返回示例:{"resultcode":"200","reason":"成功的返回","result":{"area":"浙江省温州市平阳县","sex":"男","birthday":"1989年03月08日"}}身份证号码归属地数据库调用代码XML返回示例:<root><resultcode>200</resultcode><reason>成功的返回</reason><result><area>浙江省温州市平阳县</area><sex>男</sex><birthday>1989年03月08日</birthday></result></root>。
python实现获取⾝份证号码的⽅法 记录瞬间1.号码的结构 公民⾝份号码是特征组合码,由⼗七位数字本体码和⼀位校验码组成。
排列顺序从左⾄右依次为:六位数字地址码,⼋位数字出⽣⽇期码,三位数字顺序码和⼀位数字校验码。
2.地址码 表⽰编码对象常住户⼝所在县(县级市、旗、区)的⾏政区划代码,按GB/T2260的规定执⾏。
3.出⽣⽇期码 表⽰编码对象出⽣的年、⽉、⽇,按GB/T7408的规定执⾏,年、⽉、⽇代码之间不⽤分隔符。
4.顺序码 表⽰在同⼀地址码所标识的区域范围内,对同年、同⽉、同⽇出⽣的⼈编定的顺序号,顺序码的奇数分配给男性,偶数分配给⼥性。
5.校验码 根据前⾯⼗七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。
中国⼤陆居民⾝份证号码中的地址码的数字编码规则为: 第⼀、⼆位表⽰省(⾃治区、直辖市、特别⾏政区)。
第三、四位表⽰市(地级市、⾃治州、地区、盟及直辖市所属区和县的汇总码)。
其中,01-20,51-70表⽰地级市;21-50表⽰地区(⾃治州、盟)。
第五、六位表⽰县(区、县级市、旗)。
01-18表⽰地级市、⾃治州、地区、盟辖县级市;21-80表⽰县(旗);81-99表⽰省直辖县级⾏政单位。
⽣⽇期码 (⾝份证号码第七位到第⼗四位)表⽰编码对象出⽣的年、⽉、⽇,其中年份⽤四位数字表⽰,年、⽉、⽇之间不⽤分隔符。
例如:1981年05⽉11⽇就⽤19810511表⽰。
顺序码 (⾝份证号码第⼗五位到⼗七位)地址码所标识的区域范围内,对同年、⽉、⽇出⽣的⼈员编定的顺序号。
其中第⼗七位奇数分给男性,偶数分给⼥性。
校验码 作为尾号的校验码,是由号码编制单位按统⼀的公式计算出来的,如果某⼈的尾号是0-9,都不会出现X,但如果尾号是10,那么就得⽤X来代替,因为如果⽤10做尾号,那么此⼈的⾝份证就变成了19位,⽽19位的号码违反了国家标准,并且中国的计算机应⽤系统也不承认19位的⾝份证号码。
Python查找电话号码归属地、邮编、运营商信息等# -*- coding: utf-8 -*-1. 查找单个电话号码from phone import Phonedef get_phone_info(phone_num):phone_info = Phone().find(phone_num)try:phone = phone_info['phone']province = phone_info['province'] #省city = phone_info['city'] #城市zip_code = phone_info['zip_code'] #邮编area_code = phone_info['area_code'] #区号phone_type = phone_info['phone_type'] #运营商except:print('----- the phone number is empty, please check your phone number! -----')return phone, province, city, zip_code, area_code, phone_typeif__name__ == "__main__":phone_num = '177********'#输⼊要查询的电话号码,注意只能是三⼤运营商的⼿机号码if len(phone_num)==0:print('please input your phone number first!')phone_num = input("phone number:")phone_info = get_phone_info(phone_num) #列表返回print(phone_info[0]) #电话号print(phone_info[1]) #省份print(phone_info[2]) #城市print(phone_info[3]) #邮编print(phone_info[4]) #区号print(phone_info[5]) #运营商2. 查找excel⾥⾯的电话号码from phone import Phoneimport xlrdimport osimport xlsxwriterdef get_phone_info(phone_num):phone_info = Phone().find(phone_num)try:phone = phone_info['phone']province = phone_info['province'] #省city = phone_info['city'] #城市zip_code = phone_info['zip_code'] #邮编area_code = phone_info['area_code'] #区号phone_type = phone_info['phone_type'] #运营商except:print('----- the phone number is empty, please check your phone number! -----')return phone, province, city, zip_code, area_code, phone_typedef get_phone_info_excel(phone_number_path, sheet_num = 0):#解析地址file_name = os.path.splitext(phone_number_path)[0]file_realname = file_name.split('\\')[-1] #⽂件名print(file_realname)myWorkbook = xlsxwriter.Workbook(file_name+'_get_info.xlsx')#创建⼀个excel⽂件,⽤于待会写数据sheet_file_realname = myWorkbook.add_worksheet(file_realname)#在⽂件中创建⼀个名为file_realname的sheet,不加名字默认为sheet1bold= myWorkbook.add_format({'bold':True})#设置⼀个加粗的格式对象# workfomat = workbook.add_format({# 'bold' : True, #字体加粗# 'border' : 1, #单元格边框宽度# 'align' : 'center', #对齐⽅式# 'valign' : 'vcenter', #字体对齐⽅式# 'fg_color' : '#F4B084', #单元格背景颜⾊# })sheet_file_realname.write(0, 0, u'电话号',bold) #给第⼀⾏加标签sheet_file_realname.write(0, 1, u'省份',bold)sheet_file_realname.write(0, 2, u'城市',bold)sheet_file_realname.write(0, 3, u'邮编',bold)sheet_file_realname.write(0, 4, u'区号',bold)sheet_file_realname.write(0, 5, u'运营商',bold)#设置读Excel的编码格式xlrd.Book.encoding = "gbk"rb = xlrd.open_workbook(phone_number_path)sheet = rb.sheet_by_index(sheet_num) #读第⼏个sheet,默认为第⼀个nrows = sheet.nrowsfor i in range(nrows):phone_num = int(sheet.cell_value(i, 0)) #假设电话号码在第⼀列,从第⼀⾏开始,也就是索引(0,0),#int是因为读⼊的时候⾃动变成float,会超过电话号码长度phone_info = get_phone_info(phone_num)sheet_file_realname.write(i + 1, 0, phone_info[0])sheet_file_realname.write(i + 1, 1, phone_info[1])sheet_file_realname.write(i + 1, 2, phone_info[2])sheet_file_realname.write(i + 1, 3, phone_info[3])sheet_file_realname.write(i + 1, 4, phone_info[4])sheet_file_realname.write(i + 1, 5, phone_info[5])myWorkbook.close()if__name__ == "__main__":phone_number_path = r'D:\Python_workspace\spyder_space\电话号码.xlsx'get_phone_info_excel(phone_number_path)3. 安装phone包:pip install phone4. 如果电话号码查不到信息或者输⼊的不在解析范围内,会报错:UnboundLocalError: local variable 'phone' referenced before assignment,那是因为并没有执⾏get_phone_info函数的try语句,却return了try语句中的参数造成的。
python实现⾝份证实名认证的⽅法实例前⾔本⽂主要给⼤家介绍了关于python实现⾝份证实名认证的⽅法,⽂中通过⽰例代码介绍的⾮常详细,下⾯话不多说了,来⼀起看看详细的介绍吧⽅法如下⼀、⾸先我们选⽤了阿⾥云的⾝份证实名认证接⼝:⼆、编译并运⾏源代码import requestsdef reqeust_yueyuan(name,id_card):url = 'https:///idcard'params={'idCard': id_card, #查询的⾝份证号码'name':name #名字}appcode='但那个你购买api接⼝时就会有appcode看第⼀步'headers={"Authorization":"APPCODE "+appcode}try:resp = requests.get(url=url, params=params, headers=headers)except:return Noneif resp.status_code == 200:content = resp.json()return contentreturn Nonedata_js = reqeust_yueyuan('林xx',440582199412xxxxxx)print(data_js)#结果{'status': '01', 'msg': '实名认证通过!', 'idCard': 'xxxxxxxxxxxx', 'name': 'xxx', 'sex': '男', 'area': '⼴东省潮阳市', 'province': '⼴东省', 'city': '潮阳市', 'prefecture': '', 'birthday': '1994-12-09', 'addrCode': 'xxxx', 'lastCode': '1'}总结是不是很简单就实现了⾝份证的认证,也可以集成到Django、Flask等框架⾥。
Python实现⾝份证号码解析中国的居民⾝份证有18位。
其中前17位是信息码,最后1位是校验码。
每位信息码可以是0-9的数字,⽽校验码可以是0-9或X,其中X表⽰10。
⾝份证校验码算法:设18位⾝份证号序列从左到右为:引⽤a[0], a[1], a[2], a[3], ..., a[16], a[17]其中a[i]表⽰第i位数字,i=0,1,2,...,17,如果最后⼀位(校验位)是X,则a[17]=10每⼀位被赋予⼀个“权值”,其中,第i位的权值w[i]的计算⽅法是:引⽤w[i] = 2**(17-i) % 11其中,i=0,1,2,3,...,17,运算符按Python惯例:x**y表⽰x的y次⽅,x%y表⽰x除以y的余数。
如果⼀个⾝份证号是正确的,那么:引⽤(a[0]*w[0] + a[1]*w[1] + a[2]*w[2] + ... + a[16]*w[16] + a[17]*w[17]) % 11 == 1实际上,校验位a[17]的计算⽅法,就是巧妙地选择⼀个值使得上式成⽴。
根据上述算法,下⾯是⼀个验证⾝份证号正确性的程序。
初学者————代码没有什么依照编写规范,流⽔账的模式。
还有两个功能没有实现:1、依照⾝份证号码的区域代码解析所在区域;2、将⾝份证校验码的校验作为前置判断,如果错误就不再解析其他内容,汗,我还不会;ID=input('请输⼊⼗⼋位⾝份证号码: ')if len(ID)==18:print("你的⾝份证号码是 "+ID)else:print("错误的⾝份证号码")ID_add=ID[0:6]ID_birth=ID[6:14]ID_sex=ID[14:17]ID_check=ID[17]#ID_add是⾝份证中的区域代码,如果有⼀个⾏政区划代码字典,就可以⽤获取⼤致地址#year=ID_birth[0:4]moon=ID_birth[4:6]day=ID_birth[6:8]print("⽣⽇: "+year+'年'+moon+'⽉'+day+'⽇')if int(ID_sex)%2==0:print('性别:⼥')else:print('性别:男')#此部分应为错误判断,如果错误就不应有上⾯的输出,如何实现?#W=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]ID_num=[18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2]ID_CHECK=['1','0','X','9','8','7','6','5','4','3','2']ID_aXw=0for i in range(len(W)):ID_aXw=ID_aXw+int(ID[i])*W[i]ID_Check=ID_aXw%11if ID_check==ID_CHECK[ID_Check]:print('正确的⾝份证号码')else:print('错误的⾝份证号码')我们再来看⼀个更加完善些的⽰例import re#Errors=['验证通过!','⾝份证号码位数不对!','⾝份证号码出⽣⽇期超出范围或含有⾮法字符!','⾝份证号码校验错误!','⾝份证地区⾮法!']def checkIdcard(idcard):Errors=['验证通过!','⾝份证号码位数不对!','⾝份证号码出⽣⽇期超出范围或含有⾮法字符!','⾝份证号码校验错误!','⾝份证地区⾮法!']area={"11":"北京","12":"天津","13":"河北","14":"⼭西","15":"内蒙古","21":"辽宁","22":"吉林","23":"⿊龙江","31":"上海","32":"江苏","33":"浙江","34":"安徽","35":"福建","36":"江西","37":"⼭东","41":"河南","42":"湖北","43":"湖南","44":"⼴东","45":"⼴西","46":"海南"," idcard=str(idcard)idcard=idcard.strip()idcard_list=list(idcard)#地区校验if(not area[(idcard)[0:2]]):print Errors[4]#15位⾝份号码检测if(len(idcard)==15):if((int(idcard[6:8])+1900) % 4 == 0 or((int(idcard[6:8])+1900) % 100 == 0 and (int(idcard[6:8])+1900) % 4 == 0 )):erg=pile('[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$')#//测试出⽣⽇期的合法性else:ereg=pile('[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$')#//测试出⽣⽇期的合法性if(re.match(ereg,idcard)):print Errors[0]else:print Errors[2]#18位⾝份号码检测elif(len(idcard)==18):#出⽣⽇期的合法性检查#闰年⽉⽇:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))#平年⽉⽇:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))if(int(idcard[6:10]) % 4 == 0 or (int(idcard[6:10]) % 100 == 0 and int(idcard[6:10])%4 == 0 )):ereg=pile('[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$')#//闰年出⽣⽇期的合法性正则表达式else:ereg=pile('[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$')#//平年出⽣⽇期的合法性正则表达式#//测试出⽣⽇期的合法性if(re.match(ereg,idcard)):#//计算校验位S = (int(idcard_list[0]) + int(idcard_list[10])) * 7 + (int(idcard_list[1]) + int(idcard_list[11])) * 9 + (int(idcard_list[2]) + int(idcard_list[12])) * 10 + (int(idcard_list[3]) + int(idcard_list[13])) * 5 + (int(idcard_list[4]) + int(idcard_list[14])) * 8 + (int(idcard_list[5]) + int(idca Y = S % 11M = "F"JYM = "10X98765432"M = JYM[Y]#判断校验位if(M == idcard_list[17]):#检测ID的校验位print Errors[0]else:print Errors[3]else:print Errors[2]else:print Errors[1]可以通过命令⾏输⼊。
python3通过纯真IP数据库查询IP归属地信息在⽹上看到的别⼈写的python2的代码,修改成了python3。
把纯真IP数据库⽂件qqwry.dat放到czip.py同⼀⽬录下。
1#! /usr/bin/env python2# -*- coding: utf-8 -*-3# filename: czip.py456import socket7import struct8910class CzIp:11def__init__(self, db_file='qqwry.dat'):12 self.f_db = open(db_file, "rb")13 bs = self.f_db.read(8)14 (self.first_index, st_index) = struct.unpack('II', bs)15 self.index_count = int((st_index - self.first_index) / 7 + 1)16 self.cur_start_ip = None17 self.cur_end_ip_offset = None18 self.cur_end_ip = None19# print(self.get_version(), " 纪录总数: %d 条 "%(self.index_count))2021def get_version(self):22'''23获取版本信息,最后⼀条IP记录 255.255.255.0-255.255.255.255 是版本信息24 :return: str25'''26 s = self.get_addr_by_ip(0xffffff00)27return s2829def _get_area_addr(self, offset=0):30if offset:31 self.f_db.seek(offset)32 bs = self.f_db.read(1)33 (byte,) = struct.unpack('B', bs)34if byte == 0x01 or byte == 0x02:35 p = self.getLong3()36if p:37return self.get_offset_string(p)38else:39return""40else:41 self.f_db.seek(-1, 1)42return self.get_offset_string(offset)4344def _get_addr(self, offset):45'''46获取offset处记录区地址信息(包含国家和地区)47如果是中国ip,则是 "xx省xx市 xxxxx地区" 这样的形式48 (⽐如:"福建省电信", "澳⼤利亚墨尔本Goldenit有限公司")49 :param offset:50 :return:str51'''52 self.f_db.seek(offset + 4)53 bs = self.f_db.read(1)54 (byte,) = struct.unpack('B', bs)55if byte == 0x01: # 重定向模式156 country_offset = self.getLong3()57 self.f_db.seek(country_offset)58 bs = self.f_db.read(1)59 (b,) = struct.unpack('B', bs)60if b == 0x02:61 country_addr = self.get_offset_string(self.getLong3())62 self.f_db.seek(country_offset + 4)63else:64 country_addr = self.get_offset_string(country_offset)65 area_addr = self._get_area_addr()66elif byte == 0x02: # 重定向模式267 country_addr = self.get_offset_string(self.getLong3())68 area_addr = self._get_area_addr(offset + 8)69else: # 字符串模式70 country_addr = self.get_offset_string(offset + 4)71 area_addr = self._get_area_addr()72return country_addr + "" + area_addr7374def dump(self, first, last):75'''76打印数据库中索引为first到索引为last(不包含last)的记录77 :param first:78 :param last:79 :return:80'''81if last > self.index_count:82 last = self.index_count83for index in range(first, last):84 offset = self.first_index + index * 785 self.f_db.seek(offset)86 buf = self.f_db.read(7)87 (ip, of1, of2) = struct.unpack("IHB", buf)88 address = self._get_addr(of1 + (of2 << 16))89print("%d %s %s" % (index, self.ip2str(ip), address)) 9091def _set_ip_range(self, index):92 offset = self.first_index + index * 793 self.f_db.seek(offset)94 buf = self.f_db.read(7)95 (self.cur_start_ip, of1, of2) = struct.unpack("IHB", buf)96 self.cur_end_ip_offset = of1 + (of2 << 16)97 self.f_db.seek(self.cur_end_ip_offset)98 buf = self.f_db.read(4)99 (self.cur_end_ip,) = struct.unpack("I", buf)100101def get_addr_by_ip(self, ip):102'''103通过ip查找其地址104 :param ip: (int or str)105 :return: str106'''107if type(ip) == str:108 ip = self.str2ip(ip)109 L = 0110 R = self.index_count - 1111while L < R - 1:112 M = int((L + R) / 2)113 self._set_ip_range(M)114if ip == self.cur_start_ip:115 L = M116break117if ip > self.cur_start_ip:118 L = M119else:120 R = M121 self._set_ip_range(L)122# version information, 255.255.255.X, urgy but useful 123if ip & 0xffffff00 == 0xffffff00:124 self._set_ip_range(R)125if self.cur_start_ip <= ip <= self.cur_end_ip:126 address = self._get_addr(self.cur_end_ip_offset) 127else:128 address = "未找到该IP的地址"129return address130131def get_ip_range(self, ip):132'''133返回ip所在记录的IP段134 :param ip: ip(str or int)135 :return: str136'''137if type(ip) == str:138 ip = self.str2ip(ip)139 self.get_addr_by_ip(ip)140 range = self.ip2str(self.cur_start_ip) + ' - ' \141 + self.ip2str(self.cur_end_ip)142return range143144def get_offset_string(self, offset=0):145'''146获取⽂件偏移处的字符串(以'\0'结尾)147 :param offset: 偏移148 :return: str149'''150if offset:151 self.f_db.seek(offset)152 bs = b''153 ch = self.f_db.read(1)154 (byte,) = struct.unpack('B', ch)155while byte != 0:156 bs += ch157 ch = self.f_db.read(1)158 (byte,) = struct.unpack('B', ch)159return bs.decode('gbk')160161def ip2str(self, ip):162'''163整数IP转化为IP字符串164 :param ip:165 :return:166'''167return str(ip >> 24) + '.' + str((ip >> 16) & 0xff) + '.' + str((ip >> 8) & 0xff) + '.' + str(ip & 0xff) 168169def str2ip(self, s):170'''171 IP字符串转换为整数IP172 :param s:173 :return:174'''175 (ip,) = struct.unpack('I', socket.inet_aton(s))176return ((ip >> 24) & 0xff) | ((ip & 0xff) << 24) | ((ip >> 8) & 0xff00) | ((ip & 0xff00) << 8)177178def getLong3(self, offset=0):179'''180 3字节的数值181 :param offset:182 :return:183'''184if offset:185 self.f_db.seek(offset)186 bs = self.f_db.read(3)187 (a, b) = struct.unpack('HB', bs)188return (b << 16) + a189190191192if__name__ == '__main__':193 cz = CzIp()194print(cz.get_version())195 ip = '14.215.177.39'196print(cz.get_ip_range(ip))197print(cz.get_addr_by_ip(ip))运⾏结果:。
ABCDEFYYYYMMDDXXXR1. 地址码(ABCDEF):表示编码对象常住户口所在县市、旗、区的行政区划代码,按GB/T2260 的规定执行。
2. 出生日期码(YYYYMMDD):表示编码对象出生的年、月、日,按GB/T7408 的规定执行,年、月、日分别用 4 位、2 位(不足两位加0)、2(同上)位数字表示,之间不用分隔符。
3. 顺序码(XXX):表示在同一地址码所标识的区域范围内对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。
4.校验码(R):一位数字,通过前17 位数字按照ISO 7064:1983.MOD 11-2 校验码计算得.出。
15 位的身份证编码首先把出生年扩展为4 位,简单的就是增加一个19,但是这对于1900 年出生的人不使用(这样的寿星不多了)function isCardIDsIdvar aCity11:北京12:天津13:河北14:山西15:内蒙古21:辽宁22:吉林23:黑龙江31:上海32:江苏33:浙江34:安徽35:福建36:江西37:山东41:河南42:湖北43:湖南44:广东45:广西46:海南50:重庆51:四川52:贵州53:云南54:西藏61:陕西62:甘肃63:青海64:宁夏65:新疆71:台湾81:香港82:澳门91:国外var iSum0 var info if/d17dx/i.testsId return false// 你输入的身份证长度或格式错误sIdsId.replace/x/ia ifaCityparseIntsId.substr02null return false //你的身份证地区非法sBirthdaysId.substr64-NumbersId.substr102-NumbersId.substr122 var dnew DatesBirthday.replace/-/g/ ifsBirthdayd.getFullYear- d.getMonth1 - d.getDatereturn false //身份证上的出生日期非法forvar i 17i0i -- iSum Math.pow2i 11 parseIntsId.charAt17 - i11 ifiSum111 return false //你输入的身份证号非法return true//aCityparseIntsId.substr02sBirthdaysId.substr1612 男:女验证身份证校验的计算方式:1. 对前17 位数字本体码加权求和公式为:S SumAi Wi i 0 ... 16 其中Ai 表示第i 位置上的身份证号码数值,Wi 表示第i 位置上的加权因子其各位对应的值依次为:7 9 10 5 8 4 2 1 63 7 9 10 5 8 4 2 2. 以11 对计算结果取模Y modS 11 3. 根据模的值得到对应的校验码对应关系为:Y 值:0 1 2 3 4 5 6 7 8 9 10 校验码:1 0 X 9 8 7 6 5 4 3 2 好象很复杂但具体的说也很简单.便是: 1.将前面的身份证号码17 位数分别乘以不同的系数。
基于Python的身份证号码归属地数据库调用代码实例
代码描述:基于Python的身份证号码归属地数据库调用代码实例
关联数据:身份证查询
接口平台:聚合数据
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode
#----------------------------------
# 身份证查询调用示例代码-聚合数据
# 在线接口文档:/docs/38
#----------------------------------
def main():
#配置您申请的APPKey
appkey ="*********************"
#1.身份证信息查询
request1(appkey,"GET")
#2.身份证泄漏查询
request2(appkey,"GET")
#3.身份证挂失查询
request3(appkey,"GET")
#身份证信息查询
def request1(appkey, m="GET"):
url ="/idcard/index"
params ={
"cardno": "", #身份证号码
"dtype": "", #返回数据格式:json或xml,默认json
"key": appkey, #你申请的key
}
params =urlencode(params)
if m =="GET":
f =urllib.urlopen("%s?%s"%(url, params))
else:
f =urllib.urlopen(url, params)
content =f.read()
res =json.loads(content)
if res:
error_code =res["error_code"]
if error_code ==0:
#成功请求
print res["result"]
else:
print"%s:%s"%(res["error_code"],res["reason"]) else:
print"request api error"
#身份证泄漏查询
def request2(appkey, m="GET"):
url ="/idcard/leak"
params ={
"cardno": "", #身份证号码
"dtype": "", #返回数据格式:json或xml,默认json
"key": appkey, #你申请的key
}
params =urlencode(params)
if m =="GET":
f =urllib.urlopen("%s?%s"%(url, params))
else:
f =urllib.urlopen(url, params)
content =f.read()
res =json.loads(content)
if res:
error_code =res["error_code"]
if error_code ==0:
#成功请求
print res["result"]
else:
print"%s:%s"%(res["error_code"],res["reason"]) else:
print"request api error"
#身份证挂失查询
def request3(appkey, m="GET"):
url ="/idcard/loss"
params ={
"cardno": "", #身份证号码
"dtype": "", #返回数据格式:json或xml,默认json
"key": appkey, #你申请的key
}
params =urlencode(params)
if m =="GET":
f =urllib.urlopen("%s?%s"%(url, params))
else:
f =urllib.urlopen(url, params)
content =f.read()
res =json.loads(content)
if res:
error_code =res["error_code"]
if error_code ==0:
#成功请求
print res["result"]
else:
print"%s:%s"%(res["error_code"],res["reason"]) else:
print"request api error"
if__name__ =='__main__':
main()。