时间: 下载该word文档
给定公司名称excel列表,在天眼查搜索爬取企业工商信息
上一篇写了天眼查公司详情页单页面爬取公司基本信息(简单爬取天眼查数据),这里改进一步。需求提供公司名称,需要获取公司工商注册信息,欲查找的公司名称存储在excel里。步骤分解1.读取excel数据(1)打开工作簿import xlrddef open_excel(: book = xlrd.open_workbook( bookexcept Exception as e:print ('打开工作簿'+file+'出错:'+str(e(2)读取工作簿中所有工作表def read_sheets(: book = open_excel(file sheets = book.sheets(return sheetsexcept Exception as e:print ('读取工作表出错:'+str(e(3)读取某一工作表中数据某一列的数据def read_data(sheet, n=0: dataset = []for r in range(sheet.nrows: col = sheet.cell(r, n.value dataset.append(colreturn dataset2.打开浏览器因为是采用selenium+浏览器,首先先打开浏览器(可用火狐、谷歌等浏览器,只要配置好环境),这里采用了phantomjs这个无头浏览器,并使用了useragent代理。此外,service_args参数可调用ip代理。from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdef driver_open(: dcap = dict(DesiredCapabilities.PHANTOMJS
dcap['phantomjs.page.settings.userAgent'] = ('Mozilla/5.0 (Windows NT 6.1; WOW64 AppleWebKit/537.36 (KHTML, like Gecko Chrome/52.0.2743.82 Safari/537.36' service_args = ['--proxy=122.228.179.178:80','--proxy-type=http' ] open_driver = webdriver.PhantomJS(executable_path='D:/Anaconda2/phantomjs.exe', desired_capabilities=dcap# , service_args=service_args # ip代理 return open_driver3.获取网页源代码根据提供的url和等待加载时间,返回网页源码的BeautifulSoup对象。欲获取源代码的网页url有两种类型,一种是搜索结果页面的url,一种是公司详情页的url。根据分析,公司详情页的url末尾是一串数字,表示公司的id,这个id是要保存下来,作为唯一识别码,因此增加一步判断,判断url是否为公司详情页的url(最后一个'/'后面全是数字), 如果是,同时返回公司的id。import timefrom bs4 import BeautifulSoupdef get_content(url, waiting=3: open_driver = driver_open( open_driver.get(url# 等待waiting秒,使js加载,时间可延长 time.sleep(waiting# 获取网页内容
content = open_driver.page_source.encode('utf-8'print content open_driver.close( res_soup = BeautifulSoup(content, 'lxml'
com_id = url.split('/'[-1]if com_id.isdigit(:return res_soup, com_idelse:return res_soup4.根据搜索词搜索如果输入的公司名,能查到完全对应的公司名(可能是曾用名),则返回其url地址,
如果不存在,则返回空。import urllibdef search(keyname: key = urllib.quote(keyname search_url = '' + key + '&checkFrom=searchBox'# print search_url res_soup = get_content(search_url# print res_soup# 解析搜索结果的第一个记录
ifname = res_soup.select('div.search_result_single > div.row > div > a > span' name = ifname[0].text if len(ifname > 0 else None ifcym = res_soup.find_all(attrs={'ng-bind-html': 'node.historyNames'} cym = ifcym[0].text if len(ifcym > 0 else Noneif name == keyname or cym == keyname: company_url = res_soup.select('div.search_result_single > div.row > div > a '[0].get('href'return company_urlelse:# print '不存在该公司'return5.公司详情页数据获取并存储(1)在数据库中建表首先分析要获取哪些数据,并设计好表,我这里共涉及3张表(采用mysql)。其一,基础信息CREATE TABLE `tianyancha_qyjc` (`id` int(11 NOT NULL AUTO_INCREMENT,`nbxh` varchar(20 DEFAULT NULL comment 't+天眼查上公司id',`qymc` varchar(50 DEFAULT
NULL comment '企业名称',`qyzch` varchar(20 DEFAULT NULL comment '工商注册号',`fddbr` varchar(50 DEFAULT NULL comment '法定代表人',`qylx` varchar(50 DEFAULT NULL comment '企业类型',`zt` varchar(20 DEFAULT NULL comment '状态',`zczb` varchar(50 DEFAULT NULL comment '注册资本',`zs` varchar(100 DEFAULT NULL comment '注册地址',`jyfw` varchar(1000 DEFAULT NULL comment '经营范围',`gxdw` varchar(50 DEFAULT NULL comment '登记机构',`xkksrq` varchar(12 DEFAULT NULL comment '营业开始时间',`xkjsrq` varchar(20 DEFAULT NULL comment '营业结束时间',`slrq` varchar(20 DEFAULT NULL comment '注册日期',`hy` varchar(50 DEFAULT NULL comment '行业',`zzjgdm` varchar(20 DEFAULT NULL comment '组织机构代码',`hzrq` varchar(20 DEFAULT NULL comment '核准日期',`tyshxydm` varchar(20 DEFAULT NULL comment '统一社会信用代码',`cym` varchar(50 DEFAULT NULL comment '曾用名',`rksj` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '获取时间', PRIMARY KEY (`id` ENGINE=MyISAM DEFAULT CHARSET=utf8;其二,高管信息CREATE TABLE `tianyancha_gg` (`id` int(11 NOT NULL AUTO_INCREMENT,`nbxh` varchar(20 DEFAULT NULL comment 't+天眼查上公司id',`xm` varchar(20 DEFAULT
NULL comment '姓名',`zw` varchar(30 DEFAULT NULL comment '职务',`rksj` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '获取时间', PRIMARY KEY (`id` ENGINE=MyISAM DEFAULT CHARSET=utf8;其三,投资信息(包含股东和对外投资)CREATE TABLE `tianyancha_tzf` (`id` int(11 NOT NULL AUTO_INCREMENT,`nbxh` varchar(20 DEFAULT NULL comment 't+天眼查上投资方公司id,如果股东为自然人,则为空',`xm` varchar(50 DEFAULT NULL comment '姓名/企业名称',`btzfnbxh` varchar(20 DEFAULT NULL comment 't+天眼查上被投资公司id',`rksj` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment '获取时间', PRIMARY KEY (`id` ENGINE=MyISAM DEFAULT CHARSET=utf8;(2)连接mysql数据库,写插入数据函数import MySQLdbconn = MySQLdb.connect(host='localhost', port=3306, db='test', user='root', passwd='admin23', charset='utf8'def insert_data(sql, datas: cursor = conn.cursor( cursor.execute(sql, datas cursor.close( conn.commit((3)获取基础信息,并插入数据库表