统一社会信用代码校验python实现

新版本营业执照的统一社会信用代码的规则,

# -*- coding: utf-8 -*-
'''
Created on 2017年4月5日
18位统一社会信用代码从2015年10月1日正式实行

@author: dzm
'''
# 统一社会信用代码中不使用I,O,Z,S,V
SOCIAL_CREDIT_CHECK_CODE_DICT = {
                '0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,
                'A':10,'B':11,'C':12, 'D':13, 'E':14, 'F':15, 'G':16, 'H':17, 'J':18, 'K':19, 'L':20, 'M':21, 'N':22, 'P':23, 'Q':24,
               'R':25, 'T':26, 'U':27, 'W':28, 'X':29, 'Y':30}
# GB11714-1997全国组织机构代码编制规则中代码字符集
ORGANIZATION_CHECK_CODE_DICT = {
                '0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,
                'A':10,'B':11,'C':12, 'D':13, 'E':14, 'F':15, 'G':16, 'H':17,'I':18, 'J':19, 'K':20, 'L':21, 'M':22, 'N':23, 'O':24,'P':25, 'Q':26,
               'R':27,'S':28, 'T':29, 'U':30,'V':31, 'W':32, 'X':33, 'Y':34,'Z':35}

class UnifiedSocialCreditIdentifier(object):
    '''
    统一社会信用代码
    '''

    def __init__(self):
        '''
        Constructor
        '''
    def check_social_credit_code(self,code):
        '''
        校验统一社会信用代码的校验码
        计算校验码公式:
            C9 = 31-mod(sum(Ci*Wi),31),其中Ci为组织机构代码的第i位字符,Wi为第i位置的加权因子,C9为校验码
        '''
        # 第i位置上的加权因子
        weighting_factor = [1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28]
        # 本体代码
        ontology_code = code[0:17]
        # 校验码
        check_code = code[17]
        # 计算校验码
        tmp_check_code = self.gen_check_code(weighting_factor, ontology_code, 31, SOCIAL_CREDIT_CHECK_CODE_DICT)
        if tmp_check_code==check_code:
            return True
        else:
            return False

    def check_organization_code(self,code):    
        '''
        校验组织机构代码是否正确,该规则按照GB 11714编制
        统一社会信用代码的第9~17位为主体标识码(组织机构代码),共九位字符
        计算校验码公式:
            C9 = 11-mod(sum(Ci*Wi),11),其中Ci为组织机构代码的第i位字符,Wi为第i位置的加权因子,C9为校验码
        @param  code: 统一社会信用代码
        '''
        # 第i位置上的加权因子
        weighting_factor = [3,7,9,10,5,8,4,2]
        # 第9~17位为主体标识码(组织机构代码)
        organization_code = code[8:17]
        # 本体代码
        ontology_code=organization_code[0:8]
        # 校验码
        check_code = organization_code[8]
        # 
        print organization_code,ontology_code,check_code
        # 计算校验码
        tmp_check_code = self.gen_check_code(weighting_factor, ontology_code, 11, ORGANIZATION_CHECK_CODE_DICT)
        if tmp_check_code==check_code:
            return True
        else:
            return False

    def gen_check_code(self,weighting_factor,ontology_code, modulus,check_code_dict):
        '''
        @param weighting_factor: 加权因子
        @param ontology_code:本体代码
        @param modulus:  模数
        @param check_code_dict: 字符字典
        '''
        total = 0
        for i in range(len(ontology_code)):
            if ontology_code[i].isdigit():
                print ontology_code[i] ,weighting_factor[i]
                total += int(ontology_code[i]) * weighting_factor[i]
            else:
                total += check_code_dict[ontology_code[i]]*weighting_factor[i]
        diff = modulus - total % modulus
        print diff
        return check_code_dict.keys()[check_code_dict.values()[diff]]



if __name__ == '__main__':
    u = UnifiedSocialCreditIdentifier()
    print u.check_organization_code(code='91421126331832178C')    
    print u.check_social_credit_code(code='91420100052045470K')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

如果按照天眼查是怎么获得企业工商信息的?中说的先自己根据规则组合统一社会信用代码,再通过社会信用代码向国家企业信用信息公示系统发起爬虫请求,那么至少有 
4*12*4000*pow(35,8)=432360075000000000种组合,这是要逆天啊,显然通过这种方式不靠谱

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/warrah/article/details/69338912
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值