记录一次利用python调用chrome爬取天眼查网址工商信息的过程
1.首先准备工作:
1.pyhton3.6(官网有下载)。https://www.python.org/downloads/release/python-363/
2.pycharm 2017 开发工具。(官网有下载,破解方法百度)。
下载地址:https://www.jetbrains.com/zh/pycharm/specials/pycharm/pycharm.html
破解方法参考:http://blog.csdn.net/u014044812/article/details/78727496
3.看看谷歌浏览器的版本然后下载对应的驱动,放到pyhton安装目录下。(因为本次爬虫由python代码调用chorme浏览器)
要下载哪个版本可以参考:http://blog.csdn.net/huilan_same/article/details/51896672
下载驱动的网址有时候需要梯子
4.验证安装完成。有如下提示,代表安装完成
5.输入以下两个安装命令(pip install selenium; pip install pymysql;),因为爬虫程序要用到以下两个包。
我的已经安装过了,提示跟你的可能不一样。
2.数据库
捉取到的数据保存到mysql中,以下是我的两个表,实际需求实际分析
company表是任务列表(done字段 null代表待执行的任务,0代表失败,1代表成功,2代表没有查询到数据),
result是保存结果的表
3.代码
附上代码:
example:
import re
from selenium import webdriver
import time
import uuid
import mysqlDaoclass mainAll(object):def __init__(self):self.url = 'https://www.tianyancha.com/login'self.username = '' #自己的天眼查账号self.password = '' #自己的密码self.word = '淘宝'self.driver = self.login()# self.getData(self.driver)# self.scrapy(self.driver)print("ok,the work is done!")def login(self):# driver = webdriver.Chrome()chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--headless')driver = webdriver.Chrome(chrome_options=chrome_options)driver.get(self.url)# 模拟登陆driver.find_element_by_xpath(".//*[@id='web-content']/div/div/div/div[2]/div/div[2]/div[2]/div[2]/div[2]/input"). \send_keys(self.username)driver.find_element_by_xpath(".//*[@id='web-content']/div/div/div/div[2]/div/div[2]/div[2]/div[2]/div[3]/input"). \send_keys(self.password)driver.find_element_by_xpath(".//*[@id='web-content']/div/div/div/div[2]/div/div[2]/div[2]/div[2]/div[5]").click()time.sleep(3)driver.refresh()# driver.get('https://www.tianyancha.com/company/28723141')# # 模拟登陆完成,输入搜索内容driver.find_element_by_xpath(".//*[@id='home-main-search']").send_keys(self.word) # 输入搜索内容driver.find_element_by_xpath(".//*[@class='input-group-addon search_button']").click() # 点击搜索driver.implicitly_wait(10)#### # 选择相关度最高的搜索结果 第一条搜索框,然后再# tag = driver.find_elements_by_xpath("//div[@class='search_right_item ml10']")# btn = tag[0].find_element_by_tag_name('a')# print(btn);# closeBtn = driver.find_element_by_id("bannerClose")# if not closeBtn is None:# closeBtn.click()# btn.click()# driver.implicitly_wait(5)## # 转化句柄# now_handle = driver.current_window_handle# all_handles = driver.window_handles# for handle in all_handles:# if handle != now_handle:# # 输出待选择的窗口句柄# print(handle)# driver.switch_to.window(handle)cons = conn_mysql.selectUnFinishCompany();for row in cons:driver.implicitly_wait(3)id = row[0]conpanyname = row[1]time.sleep(1)print("正在查询第"+str(id)+"个【"+conpanyname + "】")try:self.refsh(driver, id, conpanyname);except:print("出现异常!!!第" + str(id) + "个【" + conpanyname + "】")conn_mysql.updateConpanyFlase(id);driver.refresh();print("已经完成第" + str(id) + "个【" + conpanyname + "】")return driverdef refsh(self,driver,id,conpanyname):# 模拟登陆完成,输入搜索内容driver.find_element_by_xpath("//input[@class='search_input form-control ""search_form input search-input-v1 f12 ""js-live-search']").clear();driver.find_element_by_xpath("//input[@class='search_input form-control ""search_form input search-input-v1 f12 ""js-live-search']").send_keys(conpanyname) # 输入搜索内容driver.find_element_by_xpath("//div[@class='input-group-addon button-blue-sm pt0 pb0']").click() # 点击搜索driver.implicitly_wait(10)# 选择相关度最高的搜索结果 第一条搜索框,然后再tag = driver.find_elements_by_xpath("//div[@class='search_right_item ml10']")if len(tag) ==0:print("没有查询到数据!!!第" + str(id) + "个【" + conpanyname + "】")conn_mysql.updateConpanyFlase(id);return 0;btn = tag[0].find_element_by_tag_name('a')print(btn);try:closeBtn = driver.find_element_by_id("bannerClose")if not closeBtn is None:closeBtn.click()except:print("")btn.click()driver.implicitly_wait(5)# 转化句柄now_handle = driver.current_window_handleall_handles = driver.window_handlesfor handle in all_handles:if handle != now_handle:# 输出待选择的窗口句柄print(handle)driver.switch_to.window(handle)self.getData(driver,id);driver.close();driver.switch_to_window(all_handles[0])return driverdef getData(self, driver,id):re = self.baseInfo("test",driver,id);print(re);conn_mysql.insertCompany(re);conn_mysql.updateConpany(id);return 1;def baseInfo(self, idd,driver,id):# base = self.driver.find_element_by_xpath("//div[@class='company_header_width ie9Style position-rel']/div")# # base '淘宝(中国)软件有限公司浏览40770\n高新企业\n电话:18768440137邮箱:暂无\n网址:http://www.atpanel.com# # 地址:杭州市余杭区五常街道荆丰村'# name = base.text.split('浏览')[0]# tel = base.text.split('电话:')[1].split('邮箱:')[0]# email = base.text.split('邮箱:')[1].split('\n')[0]# web = base.text.split('网址:')[1].split('地址')[0]# address = base.text.split('地址:')[1]# abstract = self.driver.find_element_by_xpath("//div[@class='sec-c2 over-hide']//script")# # 获取隐藏内容# abstract = self.driver.execute_script("return arguments[0].textContent", abstract).strip()cname = driver.find_element_by_xpath("//div[@class='position-rel']/span[@class='f18 in-block vertival-middle sec-c2']").textpname = driver.find_element_by_xpath("//div[@class='f18 overflow-width sec-c3']/a").texttabs = driver.find_elements_by_tag_name('table')rows = tabs[1].find_elements_by_tag_name('tr')cols = rows[0].find_elements_by_tag_name('td' and 'th')# 工商注册号reg_code = rows[0].find_elements_by_tag_name('td')[1].text# 注册地址reg_address = rows[5].find_elements_by_tag_name('td')[1].text# 英文名称# english_name = rows[5].find_elements_by_tag_name('td')[1].text# 经营范围# ent_range = rows[6].find_elements_by_tag_name('td')[1].text# 统一信用代码creditcode = rows[1].find_elements_by_tag_name('td')[1].text# 纳税人识别号tax_code = rows[2].find_elements_by_tag_name('td')[1].text# 营业期限# deadline = rows[3].find_elements_by_tag_name('td')[1].text# 企业类型# ent_type = rows[1].find_elements_by_tag_name('td')[3].text# baseInfo = (idd, name, tel, email, web, address, abstract, reg_code, reg_address, english_name, ent_range,# creditcode, tax_code, deadline, ent_type)return ( id,cname,pname , reg_code , creditcode , reg_address , tax_code )
mysqlDao:
import pymysqldef selectUnFinishCompany():# 名称 职位 公司名称 entuidconn = pymysql.connect(host='localhost', user='root', passwd='root', db='tianyan', port=3306, charset='utf8')cur = conn.cursor() # 获取一个游标sql = "select * from company where done is null and id >= 2900 ORDER BY id desc"try:cur.execute(sql)results = cur.fetchall()return results# for row in results:# companyname = row[0]# id = row[1]# done = row[2]except:print("Error: unable to fecth data")cur.close() # 关闭游标conn.close() # 释放数据库资源def updateConpany(id):conn = pymysql.connect(host='localhost', user='root', passwd='root', db='tianyan', port=3306, charset='utf8')cur = conn.cursor() # 获取一个游标sql = "update company set done = 1 where id = '%d'"print(sql)try:cur.execute(sql % id)except:print("Error: unable to fecth data")conn.commit()cur.close() # 关闭游标conn.close() # 释放数据库资源def updateConpanyFlase(id):conn = pymysql.connect(host='localhost', user='root', passwd='root', db='tianyan', port=3306, charset='utf8')cur = conn.cursor() # 获取一个游标sql = "update company set done = 0 where id = '%d'"print(sql)try:cur.execute(sql % id)except:print("Error: unable to fecth data")conn.commit()cur.close() # 关闭游标conn.close() # 释放数据库资源def insertCompany(values):conn = pymysql.connect(host='localhost', user='root', passwd='root', db='tianyan', port=3306, charset='utf8')cur = conn.cursor() # 获取一个游标sql = "INSERT INTO result (id, companyname,pname, reg_code, creditcode , reg_address, tax_code) " \"VALUES ( '%d', '%s','%s', '%s','%s', '%s','%s') "print(sql)try:cur.execute(sql % values)except:print("Error: unable to fecth data")conn.commit()cur.close() # 关闭游标conn.close() # 释放数据库资源
记录一次利用python调用chrome爬取天眼查网址工商信息的过程相关推荐
- python爬虫爬取音乐_利用python爬虫实现爬取网易云音乐热歌榜
利用python爬虫实现爬取网易云音乐热歌榜 发布时间:2020-11-09 16:12:28 来源:亿速云 阅读:102 作者:Leah 本篇文章给大家分享的是有关利用python爬虫实现爬取网易云 ...
- mac如何用python爬网页数据_Mac——利用Python进行网页爬取
Mac--利用Python进行网页爬取 目标:利用Python爬取网页中的指定内容,例如,爬取百度百科网页中四川省的别名.html 输出:四川省的别名为:川.蜀.天府之国python 我的经验,网页爬 ...
- python爬去百度文库_利用Python语言轻松爬取数据[精品文档]
利用 Python 语言轻松爬取数据 对于小白来说,爬虫可能是一件非常复杂. 技术门槛很高的事情. 比如有人认为学爬虫必须精通 Python ,然后哼哧哼哧系统学习 Python 的每个知识点,很久之 ...
- 如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)
前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...
- python爬取百度文库_利用Python语言轻松爬取数据
利用 Python 语言轻松爬取数据 对于小白来说,爬虫可能是一件非常复杂. 技术门槛很高的事情. 比如有人认为学爬虫必须精通 Python ,然后哼哧哼哧系统学习 Python 的每个知识点,很久之 ...
- python朋友圈动态_如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)
前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...
- python抓取朋友圈动态_如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)...
原标题:如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下) 前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往 ...
- python + selenium +chrome爬取qq空间好友说说并存入mongodb数据库
python + selenium +chrome爬取qq空间好友说说并存入mongodb数据库 准备阶段 在正式开始在前需要先准备好做爬虫的工具,本例使用chrome无头浏览器进行爬取工作,也可使用 ...
- 利用python与requests爬取猫眼上的电影数据
@利用requests与pycharm爬取猫眼上排名前100的电影数据 首先是requests导包 源代码 import csv import reimport requests from reque ...
最新文章
- 基于深度学习网络的运动想象BCI系统及其应用
- python批量ping50台服务器_Python小技巧—批量ping的方法
- 怎样修改SSIS包中 Connection Manager 中的参数
- 记录奥运-当今五大Java记录框架之间的竞赛
- java生成图片url_Java Springboot如何基于图片生成下载链接
- linux yum 目录在哪,急问怎么知道yum从哪个地址下载的文件呢?
- 链表的中间节点--快慢指针
- 那些基础的线程知识,你都懂了吗?| CSDN 博文精选
- Unittest方法 -- 测试套件
- 【深入理解JS核心技术】13. 什么是高阶函数
- AB压力测试(简易版操作)
- android6.0彩蛋小游戏,安卓系统暗藏彩蛋?隐藏小游戏让你玩到停不下来
- linux下删除一个环境变量,Linux 添加环境变量和删除环境变量
- eclipse中如何把中文变成英文?
- resultful使用
- python获取今日头条搜索信息_python爬虫(十二、爬取今日头条关键词所有文章)
- VIVADO软件学习
- 求n的阶乘及1~n的阶乘之和
- 情侣相处最佳模式,写得很有道理~
- cmake错误集锦:unkown arguments specified
热门文章
- ASPX一句话木马详细分析
- 于的繁体字有几种写法_于字的意思、于的繁体字、于的笔顺笔画、于字部首和繁体字于的意思...
- vue4+Cesium1.81.0安装及配置
- 安装pandas-profiling错误:Could not build wheels for _ which use PEP 517 and cannot be installed directly
- 软考是什么?考哪个科目容易过?
- itunes无法安装到win7系统更新服务器,Win7旗舰版电脑无法安装itunes怎么办
- 菜鸟也来编程序 Windows脚本一日通
- 台式计算机 按键盘字母键 没反应6,台式电脑键盘打出的字母不对应怎么办
- 腾讯云服务器增加网卡,腾讯云服务器(CVM主机)绑定多个弹性网卡和IP地址的方法...
- Spline样条曲线