提取第三张报告的各种信息。使用正则和xpath方法。

# coding=utf8import re,json,os
from lxml import etree
from collections import OrderedDict
from common import html,LoggerUntil,handle_parse_exception
from html_processor import  HtmlProcessorlogger = LoggerUntil(name="crcc_paser").getlog(logfilename='crcc_paser.log',loglevel=2,add_StreamHandler=1)
class HtmlFileMixin(object):def save_to_file(self):with open(self.create_file(),'wb') as f:f.write(self.html)def create_file(self):if not os.path.exists('htmldir'):os.mkdir('htmldir')html_file_name = 'htmldir' + '/' + self.name + '.html'return  html_file_nameclass CrccPaser(HtmlFileMixin):def __init__(self,html,name):self.html = htmlself.name = nameself.data = OrderedDict()self.data['name'] = nameself.selector = Noneself.text =  self._get_text()self.get_selector()def _get_text(self):text = self.html.decode('utf8')return textdef get_selector(self):self.selector = etree.HTML(self.text)def extract_user_info(self):self.data['report_no'] = re.search(u'报告编号:(.*?)</strong>', self.text).group(1).strip()self.data['query_time'] = re.search(u'查询时间:(.*?)</strong>', self.text).group(1).strip()self.data['report_time'] = re.search(u'报告时间:(.*?)</strong>', self.text).group(1).strip()self.data['crcc_name'] = re.search(u'姓名:(.*?)</strong>', self.text).group(1).strip()self.data['id_type'] = re.search(u'证件类型:(.*?)</strong>', self.text).group(1).strip()self.data['id_no'] = re.search(u'证件号码:(.*?)</strong>', self.text).group(1).strip()def extract_summary_information(self):account_num = re.search(u'<tr>\s*?<td align="left" class="p">\s*?&nbsp;账户数\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?</tr>',self.text).groups()self.data['account_num'] = self._init_num_dict(account_num)uncleared_num = re.search(u'<tr>\s*?<td align="left" class="p">\s*?&nbsp;&nbsp;&nbsp;未结清/未销户账户数\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?</tr>',self.text).groups()self.data['uncleared_num'] = self._init_num_dict(uncleared_num)overdue_num = re.search(u'<tr>\s*?<td align="left" class="p">\s*?&nbsp;发生过逾期的账户数\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?</tr>',self.text).groups()self.data['overdue_num'] = self._init_num_dict(overdue_num)overdue90_num = re.search(u'<tr>\s*?<td align="left" class="p">\s*?&nbsp;&nbsp;&nbsp;发生过90天以上逾期的账户数\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?</tr>',self.text).groups()self.data['overdue90_num'] = self._init_num_dict(overdue90_num)assure_num = re.search(u'<tr>\s*?<td align="left" class="p">\s*?&nbsp;为他人担保笔数\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?<td align="center" class="p">\s*(.*?)\s*?</td>\s*?</tr>',self.text).groups()self.data['assure_num'] = self._init_num_dict(assure_num)@staticmethoddef _init_num_dict(num_tuple):num_dict = {}num_dict['credit_card'],num_dict[' home_loans'],num_dict['other_loans'] = num_tuplereturn  num_dictdef extract_all_loan_information(self):all_loan_information = re.findall(u'<li\s*?style="list-style-type: decimal; list-style-position: outside">\s*?(\S*?)\s*?</li>', self.text)self.data['all_loan_information'] = all_loan_informationdef extract_public_records(self):if not  re.search(u'系统中没有您最近5年内的欠税记录、民事判决记录、强制执行记录、行政处罚记录及电信欠费记录。',self.text):public_records=self.selector.xpath('//table[@align="center"]//table[5]/tbody/tr[3]/td')[0].strip()   # 还不能确定具体格式,有可能造成解析中断出错   #TODOelse:public_records=[u'系统中没有您最近5年内的欠税记录、民事判决记录、强制执行记录、行政处罚记录及电信欠费记录。']self.data['public_records'] = public_recordsdef extract_query_records(self):if  not  re.search(u'系统中没有您的信用报告最近2年被查询的记录。',self.text):#query_records=selector.xpath('//table[@align="center"]//table[6]/tbody/tr[3]/td')[0].strip()query_records = re.findall(u'<tr align="center">[\s\S]*?td class="p">\s*(.*?)\s*?</td>[\s\S]*?<td class="p">\s*(.*?日)\s*?</td>[\s\S]*?<td class="p">\s*(.*?)\s*?</td>[\s\S]*?<td class="p">\s*(.*?)\s*?</td>[\s\S]*?</tr>',self.text)query_records = self._init_query_records(query_records)else:query_records =[u'系统中没有您的信用报告最近2年被查询的记录。']self.data['query_records'] = query_records@staticmethoddef _init_query_records(query_records):""":type query_records : list"""query_records_list = []for record_tuple in query_records:query_record_dict = OrderedDict()query_record_dict['no'], query_record_dict['query_date'], query_record_dict['query_person'],query_record_dict['query_reason']  = record_tuplequery_records_list.append(query_record_dict)return query_records_list@handle_parse_exceptiondef extract_all(self):self.extract_user_info()self.extract_summary_information()self.extract_all_loan_information()self.extract_public_records()self.extract_query_records()def extract_crcc(html_str,name):htmlProcessor = HtmlProcessor(html_str,name)htmlProcessor.save_to_file()   # 保存html文件
crccPaser = CrccPaser(html_str, name)# crccPaser.save_to_file()
    crccPaser.extract_all()logger.info(json.dumps(crccPaser.data, ensure_ascii=False)) # TODOreturn json.dumps(crccPaser.data, ensure_ascii=False)if __name__ == '__main__':extract_crcc(html,'小明5')

其中html第三张报告的页面源码字符串。

结果是

{"name": "小明5", "report_no": "2017122200004891965680", "query_time": "2017.12.22 11:12:32", "report_time": "2017.12.22 18:38:18", "crcc_name": "小明5", "id_type": "身份证", "id_no": "**************4337", "account_num": {" home_loans": "0", "other_loans": "2", "credit_card": "0"}, "uncleared_num": {" home_loans": "0", "other_loans": "0", "credit_card": "0"}, "overdue_num": {" home_loans": "0", "other_loans": "0", "credit_card": "0"}, "overdue90_num": {" home_loans": "0", "other_loans": "0", "credit_card": "0"}, "assure_num": {" home_loans": "0", "other_loans": "0", "credit_card": "0"}, "all_loan_information": ["2012年8月23日国家开发银行湖北省分行发放的6,000元(人民币)个人助学贷款,2014年10月已结清。", "2011年11月19日国家开发银行湖北省分行发放的6,000元(人民币)个人助学贷款,2014年10月已结清。"], "public_records": ["系统中没有您最近5年内的欠税记录、民事判决记录、强制执行记录、行政处罚记录及电信欠费记录。"], "query_records": [{"no": "1", "query_date": "2017年12月4日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "2", "query_date": "2017年11月20日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "3", "query_date": "2017年11月6日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "4", "query_date": "2017年10月20日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "5", "query_date": "2017年10月10日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "6", "query_date": "2017年9月27日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}, {"no": "7", "query_date": "2017年9月18日", "query_person": "本人", "query_reason": "本人查询(互联网个人信用信息服务平台)"}]}

可以发送到后端py java提取,也可以在webview客户端提取,客户端提取js代码如下。

/*** Created by wj49476 on 201/3/20.*/function extractReport() {//消除空格String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");};//正则防止出错String.prototype.match2 = function(regObj) {var matchArray = this.match(regObj);if (matchArray && matchArray.length == 2){return matchArray[1];}else{return '';}};//数组取下标,使值不为undefinedArray.prototype.get = function(n) {value = this[n];if (value === undefined){console.debug("取下标错误");value = '';}return value;};//css选择器function getInnerText( cssselector){var element = document.querySelector(cssselector);if(element){return element.innerText;}else {console.warn("没有找到 " + cssselector + " 的元素");return "";}}var data = {};data['SummaryInformation'] = {};var htmlStr = document.body.outerHTML;function  extractUserInfo() {data["reportNo"] = htmlStr.match2(/报告编号:(.*?)<\/strong>/);data["queryTime"] = htmlStr.match2(/查询时间:(.*?)<\/strong>/);data['reportTime'] = htmlStr.match2(/报告时间:(.*?)<\/strong>/);data['crccName'] = htmlStr.match2(/姓名:(.*?)<\/strong>/);data['idType'] = htmlStr.match2(/证件类型:(.*?)<\/strong>/);data['idNo'] = htmlStr.match2(/证件号码:(.*?)<\/strong>/);}function extractSummaryInformation() {var accountNum = htmlStr.match(/<tr>\s*?<td align="left" class="p">\s*?&nbsp;账户数\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<\/tr>/);data['SummaryInformation']['accountNum'] = _initNumDict(accountNum);var unclearedNum = htmlStr.match(/<tr>\s*?<td align="left" class="p">\s*?&nbsp;&nbsp;&nbsp;未结清\/未销户账户数\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<\/tr>/);data['SummaryInformation']['unclearedNum'] = _initNumDict(unclearedNum);var overdueNum = htmlStr.match(/<tr>\s*?<td align="left" class="p">\s*?&nbsp;发生过逾期的账户数\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<\/tr>/);data['SummaryInformation']['overdueNum'] = _initNumDict(overdueNum);var overdue90Num = htmlStr.match(/<tr>\s*?<td align="left" class="p">\s*?&nbsp;&nbsp;&nbsp;发生过90天以上逾期的账户数\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<\/tr>/);data['SummaryInformation']['overdue90Num'] = _initNumDict(overdue90Num);var assureNum = htmlStr.match(/<tr>\s*?<td align="left" class="p">\s*?&nbsp;为他人担保笔数\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<td align="center" class="p">\s*(.*?)\s*?<\/td>\s*?<\/tr>/);data['SummaryInformation']['assureNum'] = _initNumDict(assureNum);}function  _initNumDict(array) {numDict = {};numDict["creditCard"] = array.get(1);numDict["homeLoans"] = array.get(2);numDict["othreLoans"] = array.get(3);return numDict;}function extractAllLoanInformation(){var allLoanInformationG = htmlStr.match(/<li\s*?style="list-style-type: decimal; list-style-position: outside">\s*?(\S*?)\s*?<\/li>/g);var allLoanInformation = [];for (var i=0; i< allLoanInformationG.length; i++){allLoanInformation.push(allLoanInformationG[i].match(/<li\s*?style="list-style-type: decimal; list-style-position: outside">\s*?(\S*?)\s*?<\/li>/).get(1));}data['allLoanInformation'] = allLoanInformation;}function extractPublicRecords() {var publicRecords ;if (!(htmlStr.match(/系统中没有您最近5年内的欠税记录、民事判决记录、强制执行记录、行政处罚记录及电信欠费记录。/))){    //没有具体的东西,可能不确定。publicRecords = getInnerText('body > div > div > table > tbody > tr:nth-child(2) > td > table:nth-child(11)');}else{publicRecords='系统中没有您最近5年内的欠税记录、民事判决记录、强制执行记录、行政处罚记录及电信欠费记录。';}data['publicRecords'] = publicRecords;}function extractQueryRecords() {if (!(htmlStr.match(/系统中没有您的信用报告最近2年被查询的记录。/))){queryRecordsG = htmlStr.match(/<tr align="center">[\s\S]*?td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?日)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<\/tr>/g);//console.debug(queryRecordsG);queryRecords = [];for (var i=0; i<queryRecordsG.length; i++){queryRecords.push(queryRecordsG[i].match(/<tr align="center">[\s\S]*?td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?日)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<td class="p">\s*(.*?)\s*?<\/td>[\s\S]*?<\/tr>/));}//console.debug(queryRecords);data['queryRecords'] = _initQueryRecords(queryRecords);}else{data['queryRecords'] = ['系统中没有您的信用报告最近2年被查询的记录。'];}}function _initQueryRecords(queryRecords) {queryRecordsArray = [];for (var i=0; i<queryRecords.length; i++){queryRecordDict = {};queryRecordDict['no'] = queryRecords[i].get(1);queryRecordDict['queryDate'] = queryRecords[i].get(2);queryRecordDict['queryPerson'] = queryRecords[i].get(3);queryRecordDict['queryReason'] = queryRecords[i].get(4);queryRecordsArray.push(queryRecordDict);}return queryRecordsArray;}function  extractReportInner() {extractUserInfo();extractSummaryInformation();extractAllLoanInformation();extractPublicRecords();extractQueryRecords();}extractReportInner();data['htmlStr'] = Base64.encode(htmlStr);return JSON.stringify(data);
}var Base64 = {// private property_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",// public method for encodingencode: function(input) {var output = "";var chr1, chr2, chr3, enc1, enc2, enc3, enc4;var i = 0;input = Base64._utf8_encode(input);while (i < input.length) {chr1 = input.charCodeAt(i++);chr2 = input.charCodeAt(i++);chr3 = input.charCodeAt(i++);enc1 = chr1 >> 2;enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);enc4 = chr3 & 63;if (isNaN(chr2)) {enc3 = enc4 = 64;} else if (isNaN(chr3)) {enc4 = 64;}output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);}return output;},// public method for decodingdecode: function(input) {var output = "";var chr1, chr2, chr3;var enc1, enc2, enc3, enc4;var i = 0;input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");while (i < input.length) {enc1 = this._keyStr.indexOf(input.charAt(i++));enc2 = this._keyStr.indexOf(input.charAt(i++));enc3 = this._keyStr.indexOf(input.charAt(i++));enc4 = this._keyStr.indexOf(input.charAt(i++));chr1 = (enc1 << 2) | (enc2 >> 4);chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);chr3 = ((enc3 & 3) << 6) | enc4;output = output + String.fromCharCode(chr1);if (enc3 != 64) {output = output + String.fromCharCode(chr2);}if (enc4 != 64) {output = output + String.fromCharCode(chr3);}}output = Base64._utf8_decode(output);return output;},// private method for UTF-8 encoding_utf8_encode: function(string) {string = string.replace(/\r\n/g, "\n");var utftext = "";for (var n = 0; n < string.length; n++) {var c = string.charCodeAt(n);if (c < 128) {utftext += String.fromCharCode(c);} else if ((c > 127) && (c < 2048)) {utftext += String.fromCharCode((c >> 6) | 192);utftext += String.fromCharCode((c & 63) | 128);} else {utftext += String.fromCharCode((c >> 12) | 224);utftext += String.fromCharCode(((c >> 6) & 63) | 128);utftext += String.fromCharCode((c & 63) | 128);}}return utftext;},// private method for UTF-8 decoding_utf8_decode: function(utftext) {var string = "";var i = 0;var c = c1 = c2 = 0;while (i < utftext.length) {c = utftext.charCodeAt(i);if (c < 128) {string += String.fromCharCode(c);i++;} else if ((c > 191) && (c < 224)) {c2 = utftext.charCodeAt(i + 1);string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));i += 2;} else {c2 = utftext.charCodeAt(i + 1);c3 = utftext.charCodeAt(i + 2);string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));i += 3;}}return string;}};

这个是js版本,由于央行征信报告页面是拼接的,css取值不能一步到位,还必须再用正则细取,再者页面元素没有很好的标记,所以js版也是正则为主。js的match对应py的search,js的macth加g模式对应py的findall。js的search是返回个数字,要先弄清楚py和js的正则api的区别。

2、另外,使用js版本的用法是,要配合app的webview。在f12的console栏里面可以调试测试js,但那不是自动化的,尤其在多个页面跳转情况下,使用webview注入js非常方便。也可以直接在app端用httpclient对淘宝网发请求,但是征信类的项目,一般需要先登录,不依赖webview直接使用httpcliet请求淘宝登录接口的方式大批量登录任意明文的账号 密码,而不是复制ua cookie什么的(复制没什么鸟用,用户根本不知道cookie是什么,更没不用说ua参数是什么了),搞定它是天方夜谭,我没见过任何人搞定过,难度指数是五颗星。有兴趣的可以试试,不要只是嘴炮说抓包模拟就完了这么简单。

人行征信第三张报告的信息提取相关推荐

  1. 人行征信报告学习经验

    人行征信报告学习经验 1. 更新机制和人物标识的区别 二代征信报告于2020年1月20日晚间切换.相对于一代征信,二代征信的更新机制有非常明显的不同.对于一代征信而言,由于系统的处理能力不同,金融机构 ...

  2. 基于大数据风控技术,PBOC二代人行征信报告的解读哪家好

    1月19日,中国人民银行征信中心开始向社会公众和金融机构提供二代格式信用报告(以下简称二代征信)查询服务.二代征信优化了个人信息报告的数据组织模型,不再简单地按照页面展示形式描述报告的组成结构,而是聚 ...

  3. 人行征信规则衍生与制定技巧|国庆不容错过的内容①

    人行征信中的变量衍生,有以下几类:属性类.账户类.查询类.逾期类,而且如果细分而言征信报告中还分为循环贷产品与非循环贷产品,那相关的二代征信变量如何衍生,怎么生成一份强有力的规则变量?今天我们为大家稍 ...

  4. 人行征信2.0对接服务:全业务种类数据,精细您的征信业务管理!

    近年来,随着互联网金融.民间金融.消费金融的快速发展,征信查询业务呈现爆发式增长.其中,一些不符合征信接入机构条件的金融机构为满足自身对信用报告的需求,通过非法的手段获取信息主体的信用报告,导致违规查 ...

  5. 人行征信密码控件卸载后残留,导致fiddler自动关闭解决办法

    人行征信密码控件卸载后,fiddler自动关闭解决办法 前段时间去下载了人行征信查询密码输入插件.也就是 这个东西.使用完了,之后卸载了. 但是当我打开平时使用的调试工具fiddler的时候后,不能正 ...

  6. 人行征信上贷款余额指的是什么?(图文)

    人行征信上贷款余额指的是什么? 下面图片是简版征信的部分截图,看到有余额3478元,贷款余额是什么意思呢? 贷款余额:贷款人尚未归还放款人(银行)的金额总额.通俗地说,就是贷款人还欠的钱,或者说是放款 ...

  7. 人行征信报告(下)——探秘二代征信的内容

    最近一些持牌机构的同学在本月底之前都有些忙碌,根据之前的<中国人民银行征信中心关于做好二代征信系统上线准备工作的通知>要求, 2019 年 11 月 30 日前需要完成与二代征信系统之间的 ...

  8. 人行征信密码控件完美卸载(去除代理提示)

    测试过网上几种方法都无法完全卸载,还是提示代理信息. 我就重新安装回征信插件,然后 下载HiBitUninstaller软件,找到征信然后右击鼠标选择强制删除即可. 如果解决了你的问题,麻烦点个赞! ...

  9. 征信报告记录有误,如何向金融机构申请异议处理?

    您知道如何在金融机构申请异议处理?流程是怎样的?多长时间能回复?今天微众税银小编为您解答. 根据征信业管理条例,如果您认为信用报告中出现错误或遗漏的记录,可以本人向金融机构或征信中心提出异议申请. 那 ...

最新文章

  1. targetSdkVersion
  2. Android开发--Service开启,关闭,绑定,解除
  3. TCP协议三步挥手与四步挥手
  4. jQueryMobile引入文件后样式无法正常显示
  5. hive创建表---并把数据导入表中
  6. python ftp编程_【编程】Python FTP
  7. Rust : utf8与unicode码
  8. css中换行的几种方式
  9. spring 定时器功能
  10. mac的obs录制不到全屏
  11. 服务器cmd升级系统命令,02-软件升级操作指导(命令行版)
  12. 三维点云——数据标注
  13. 修改显卡型号(自娱自乐)
  14. 视频片段怎么做成gif图?快试试这2种方法
  15. html同时播放多个文件夹,怎么把很多文件一起同时放入一个文件夹内
  16. 大数相乘(数组表示)
  17. 如何修改非root用户的ulimit -n的值
  18. 智慧消防应用中多设备联动火灾报警系统
  19. 软件设计师:二周目下午真题
  20. goto是python的保留字吗_Python中的保留字

热门文章

  1. 机器学习(六)——高斯判别法(GDA)
  2. WebGL加载地图临时图层解决方法
  3. 分布式一致性协议:拜占庭将军问题
  4. 分布式 | 拜占庭将军问题
  5. 从知识营销的爆发,触达知乎的高价值人群
  6. 青岛华领区块链研究院有限公司
  7. 【2022 CCPC 桂林站 日志】部分题解
  8. 设计原则(5)-迪米特法则
  9. web服务器项目常见面试题目(C++)
  10. 东北大学计算机学硕复试分数线,东北大学2019年考研复试分数线已公布