def crawlGetUrl(conn, param, proxy): ''' 以get方式获得企业基本信息并添加到enterprise_raw表 ''' cursor = conn.cursor() cursor2 = conn.cursor() cursor4 = conn.cursor() recordExists = jTool.notExsitsRecord(cursor4, 'enterprise_raw', 'eid', param['id']) cursor4.close() if recordExists: # print param['ename']+'基本信息已存在,eid'+str(param['eid']) jTool.updateData(cursor, ' where id = '+str(param['iid'])+' limit 1', 'item_url_task', {'status': '1'}) cursor.close() conn.commit() return {'logic': True, 'rtData': recordExists} if not recordExists: # print 'get url:'+param['url']+', '+param['ename'].strip()+',id:'+param['id']+', proxy:'+proxy content = jTool.getContentByProxy(proxy, param['url']) if not content: print '获取基本信息页面内容为空或失败' return {'logic': False, 'rtData': {'type': 'getPageError', 'postPage': ' '}} recEntBaseDic = None if content: try: recEntBaseDic = getEntBase(content) except Exception, e: print '解析企业基本信息页面失败', __name__, e return {'logic': False, 'rtData': {'type': 'parsePageBaseError', 'postPage': ' '}} if not recEntBaseDic: return {'logic': False, 'rtData': {'type': 'parsePageBaseError', 'postPage': ' '}} recEntBaseDic['url'] = param['url'] recEntBaseDic['eid'] = param['id'] recEntBaseDic['postContent'] = ' ' recEntBaseDic['enterprise_name'] = param['ename'].strip('') recEntBaseDic['ctime'] = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) insertId = None try: jTool.insertData(cursor2, 'enterprise_raw', recEntBaseDic) insertId = conn.insert_id() conn.commit() print str(param['id'])+' Insert enterprise baseinfo successfully in ' + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) jTool.updateData(cursor, ' where id = '+str(param['iid'])+' limit 1', 'item_url_task', {'status': '1'}) return {'logic': True, 'rtData': insertId} except Exception, e: print 'Fail to insert baseinfo record '+ ', id is '+str(param['id'])+', proxy:'+proxy, __name__, e print recEntBaseDic return {'logic': False, 'rtData': {'type': 'insertPageError', 'postPage': ' '}}
def crawlUrl(conn, param, proxy): ''' 访问url并获得企业基本信息和信用记录并入库 ''' cursor2 = conn.cursor() cursor4 = conn.cursor() cursor5 = conn.cursor() cursor6 = conn.cursor() recordExists = jTool.notExsitsRecord(cursor4, 'enterprise_raw', 'eid', param['id']) cursor4.close() if recordExists: print param['ename']+'基本信息已存在,尝试获取记录信息 \npage url:'+param['url']+', proxy:'+proxy if not recordExists: print 'get url:'+param['url']+', '+param['ename'].strip()+',id:'+param['id']+', proxy:'+proxy content = jTool.getContentByProxy(proxy, param['url']) if not content: print '获取基本信息页面内容为空或失败' return {'logic': False, 'rtData': {'type': 'getPageError', 'postContent': ' '}} recEntBaseDic = None if content: try: recEntBaseDic = getEntBase(content) except Exception, e: print '解析企业基本信息页面失败', __name__, e return {'logic': False, 'rtData': {'type': 'parsePageBaseError', 'postContent': ' '}} if not recEntBaseDic: return {'logic': False, 'rtData': {'type': 'parsePageBaseError', 'postContent': ' '}} recEntBaseDic['url'] = param['url'] recEntBaseDic['eid'] = param['id'] recEntBaseDic['postContent'] = ' ' recEntBaseDic['enterprise_name'] = param['ename'].strip('') recEntBaseDic['ctime'] = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try: jTool.insertData(cursor2, 'enterprise_raw', recEntBaseDic) conn.commit() print ' Insert enterprise baseinfo successfully in ' + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) except Exception, e: print 'insert error ', __name__, e print 'Fail to insert record '+ ', id is '+str(param['id'])+', proxy:'+proxy return {'logic': False, 'rtData': {'type': 'insertPageError', 'postContent': ' '}}
def crawlUrl(conn, param, proxy): ''' 访问url并获得企业基本信息和信用记录并入库 ''' cursor2 = conn.cursor() cursor4 = conn.cursor() cursor5 = conn.cursor() cursor6 = conn.cursor() recordExists = jTool.notExsitsRecord(cursor4, 'enterprise_raw', 'eid', param['id']) cursor4.close() if recordExists: print '企业'+param['ename']+'基本信息已存在,直接尝试获取记录信息 \npage url:'+param['url'] if not recordExists: print 'get url:'+param['url']+', '+param['ename'].strip()+',id:'+param['id'] content = jTool.getContentByProxy(proxy, param['url']) # jTool.logit(str(content), 'errorPage.txt') if not content: print '获取基本信息页面内容为空或失败' jTool.logError('\nGet page content fail, method:get, ename:'+param['ename']+',id:'+param['id']+',url:'+ param['url']) return False recEntBaseDic = None if content: try: recEntBaseDic = getEntBase(content) except: print '解析企业基本信息页面失败' jTool.logError('\nParse page content fail, method:get, ename:'+param['ename']+',id:'+param['id']+',url:'+ param['url']) return False if not recEntBaseDic: return False recEntBaseDic['url'] = param['url'] recEntBaseDic['eid'] = param['id'] recEntBaseDic['postContent'] = ' ' recEntBaseDic['enterprise_name'] = param['ename'].strip('') recEntBaseDic['ctime'] = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try: jTool.insertData(cursor2, 'enterprise_raw', recEntBaseDic) conn.commit() print ' Insert enterprise baseinfo successfully in ' + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) except: print 'Fail to insert record '+ ', id is '+str(param['id']) jTool.logError('Fail to insert record on '+ 'url is '+param['url']+', id is '+str(id)) return False dataSupplier = "法院记录/工商记录/国税记录/质监记录/经信记录/安监记录/统计记录/环保记录/民政记录/司法记录/劳动记录/建设记录/国土记录/交通记录/发改记录/信息产业/科技记录/农业记录/林业记录/海洋渔业/物价记录/食品药品/文化记录/出版记录/广电记录/公安记录/外贸记录/外汇记录/海关记录/检验检疫/人防记录/证监记录/银监记录/保监记录/金融记录/其他记录/行业协会/机构评级/社会中介/阿里巴巴/企业自报/投诉记录/异议记录" post_data_dic = {'corpName': param['ename'], 'creditID': param['id'], 'dataSupplier': dataSupplier, 'isAllInfo': 'False', 'organizeCode': '', 'returnFunction': 'parent.putDatasAndLoad'} recEntDetailDic = {} recEntDetailDic['url'] = param['url'] recEntDetailDic['eid'] = param['id'] recEntDetailDic['enterprise_name'] = param['ename'].strip() recEntDetailDic['records'] = ' ' recEntDetailDic['ctime'] = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) contentRecord = None jTool.getField(cursor6, 'enterprise_raw', 'postContent', ' where eid = '+str(param['id'])) postContent =cursor6.fetchone() cursor6.close() if len(postContent[0])>10: print '企业细节记录已存在,跳过' return True print 'post url:'+str(param['basePostUrlip'])+', '+param['ename'].strip()+',id:'+param['id'] try: contentRecord = jTool.getContentByProxy(proxy, str(param['basePostUrlip']), post_data_dic) except: print '页面未包含详细记录或获取失败' jTool.logError('\nPost get None, ename:'+param['ename'].strip()+',id:'+param['id']+',url:'+ param['url']) return True try: contentRecord = cutContent(contentRecord) print 'Get post content OK' jTool.logit(str(contentRecord), 'errorContent.log') if contentRecord: jTool.updateData(cursor5, ' where eid = '+param['id'], 'enterprise_raw', {'postContent': str(contentRecord).decode('utf-8', 'ignore')}) cursor5.close() else: print 'POST得到页面非企业详细记录页面' jTool.logError('\nPage content error, method:post, ename:'+param['ename']+', id:'+param['id']+',url:'+ param['url']+', proxy:'+proxy) return True except: print '解析页面详细记录失败' jTool.logError('\nParse page content fail, method:post, ename:'+param['ename']+', id:'+param['id']+',url:'+ param['url']+', proxy:'+proxy) return False print 'Fetch and insert successfully in ' + str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) return True