轉 https://blog.csdn.net/N_007/article/details/78835526html
參考 CNBankCard 中國各大銀行卡號查詢java
1、支付寶接口獲取名稱git
根據 卡號 獲取 銀行信息 接口github
https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=銀行卡卡號&cardBinCheck=true
返回的結果值 ( josn 類型)json
{"bank":"CCB","validated":true,"cardType":"DC","key":"銀行卡卡號","messages":[],"stat":"ok"}
其中 bank 就是 銀行代碼api
2、銀行代碼 —> 銀行名稱
爬蟲爬取 支付寶銀行合做商 頁面信息。
查看頁面結構,以下圖所示工具
須要獲取的關鍵字段就是url
<span title="" class="icon "></span>
而後獲得銀行名稱spa
3、銀行LOGO.net
https://apimg.alipay.com/combo.png?d=cashier&t=銀行代碼
若是須要多種圖片,直接 ","逗號隔開 ,例如
https://apimg.alipay.com/combo.png?d=cashier&t=ABC,CCB
4、java 代碼實現
1.pom.xml
使用 hutool 工具包來發起 http 請求,以及後續爬蟲功能
參考文檔:Hutool
<dependency> <groupId>com.xiaoleilu</groupId> <artifactId>hutool-all</artifactId> <version>3.2.3</version> </dependency>
2.mian 方法
public static void main(String[] args) throws Exception{ String bankNo = "銀行卡號"; //銀行代碼請求接口 url String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo="+bankNo+"&cardBinCheck=true"; //發送請求,獲得 josn 類型的字符串 String result = HttpUtil.get(url); // 轉爲 Json 對象 JSONObject json = new JSONObject(result); //獲取到 bank 代碼 String bank = String.valueOf(json.get("bank")); //爬取支付寶銀行合做商頁面 String listContent = HttpUtil.get("http://ab.alipay.com/i/yinhang.htm","gb2312"); //過濾獲得須要的銀行名稱 List<String> titles = ReUtil.findAll("<span title=\"(.*?)\" class=\"icon "+bank+"\">(.*?)</span>", listContent, 2); for (String title : titles) { //打印銀行名稱 Console.log(title); } }