python 根据银行卡号获取对应的银行

# coding:utf8
import requests

bank_abb = {
    "CCB": "中国建设银行",
    "ABC": "中国农业银行",
    "ICBC": "中国工商银行",
    "BOC": "中国银行",
    "CMBC": "中国民生银行",
    "CMB": "招商银行",
    "CIB": "兴业银行",
    "BCM": "交通银行",
    "CITICIB": "中信银行",
    "PSBC": "中国邮政银行",
}


def get_bank(cardNo):
    url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json"
    params = {
        "_input_charset": "utf-8",
        "cardNo": cardNo,
        "cardBinCheck": "true",
    }
    try:
        bank = requests.get(url=url, params=params).json()["bank"]
    except:
        return '银行卡号输入有误!'
    if bank_abb.has_key(bank):
        return bank_abb[bank]
    else:
        return bank


print get_bank("6228430120000000001")

这里调用的是支付宝的接口,bank_abb银行映射不是很全,需要的自己从网上搜索补齐即可。

你可能感兴趣的:(python 根据银行卡号获取对应的银行)