# -*- coding:utf-8 -*-
# author: kevin
# CreateTime: 2018/8/16
# software-version: python 3.7 import time
from selenium import webdriver
from selenium.webdriver import Firefox
import os class GetCompanyInfo(object):
"""
爬取天眼查下的企业的信息
"""
def __init__(self):
"""
初始化爬虫执行代理,使用firefox访问
"""
self.username = ''
self.password = ''
self.options = webdriver.FirefoxOptions()
self.options.add_argument('-headless') # 无头参数
self.geckodriver = r'geckodriver'
self.driver = Firefox(executable_path=self.geckodriver, firefox_options=self.options) self.start_url = 'https://www.tianyancha.com' def login(self):
"""
登录并检查状态
:return:
"""
# try:
self.driver.get(self.start_url) #print(self.driver.get_cookies()) username = self.index_login()
username_pattern = username[:3] + ' **** ' + username[-4:]
print(username_pattern)
page = self.driver.page_source
#print(page)
is_login = page.find(username_pattern) if is_login != -1:
print('登录成功') def index_login(self):
"""
主页下的登录模式
:return:
"""
get_login = self.driver.find_elements_by_xpath('//a[@class="link-white"]')[0] # 登录/注册
print(get_login.text)
# url为login的input
get_login.click()
login_by_pwd = self.driver.find_element_by_xpath('//div[@class="bgContent"]/div[3]/div[2]/div') # 切换到手机登录
login_by_pwd.click()
input1 = self.driver.find_element_by_xpath('//div[@class="bgContent"]/div[3]/div[1]/div[2]/input') # 手机号码
input2 = self.driver.find_element_by_xpath('//div[@class="bgContent"]/div[3]/div[1]/div[3]/input') # 密码
#print(input1.get_attribute('placeholder'))
#print(input2.get_attribute('placeholder'))
username, password = self._check_user_pass()
input1.send_keys(username)
input2.send_keys(password) login_button = self.driver.find_element_by_xpath('//div[@class="bgContent"]/div[3]/div[1]/div[5]') # 点击登录
#print(login_button.text)
time.sleep(1) # 必须等待否则鉴别是爬虫
login_button.click()
return username def _check_user_pass(self):
"""
检查是否有帐号密码
:return:
"""
if self.username and self.password:
return self.username, self.password
else:
#username = input('输入您的手机号码\n')
#password = input('输入您的密码\n')
username = '***' #注册手机号
password = '***' #密码
return username, password def login_page_login(self):
"""
url:www.tianyancha.com/login
在这个url下的登录模式
:return:
"""
input1 = self.driver.find_element_by_xpath('//div[contains(@class,"in-block")'
' and contains(@class, "vertical-top")'
' and contains(@class, "float-right")'
' and contains(@class, "right_content")'
' and contains(@class, "mt50")'
' and contains(@class, "mr5")'
' and contains(@class, "mb5")'
']/div[2]/div[2]/div[2]/input') input2 = self.driver.find_element_by_xpath('//div[contains(@class,"in-block")'
' and contains(@class, "vertical-top")'
' and contains(@class, "float-right")'
' and contains(@class, "right_content")'
' and contains(@class, "mt50")'
' and contains(@class, "mr5")'
' and contains(@class, "mb5")'
']/div[2]/div[2]/div[3]/input')
print(input1.get_attribute('placeholder'))
input1.send_keys("")
print(input2.get_attribute('placeholder'))
input2.send_keys('') login_button = self.driver.find_element_by_xpath('//div[contains(@class,"in-block")'
' and contains(@class, "vertical-top")'
' and contains(@class, "float-right")'
' and contains(@class, "right_content")'
' and contains(@class, "mt50")'
' and contains(@class, "mr5")'
' and contains(@class, "mb5")'
']/div[2]/div[2]/div[5]') #print(login_button.text)
time.sleep(1)
login_button.click() def get_company_info(self, company_name):
"""
获取想要的公司信息
:param company_name:
:param company_onwer:
:return:
"""
time.sleep(1)
page = self.driver.page_source index_input_company = self.driver.find_element_by_xpath('//input[@id="home-main-search"]') # 主页搜索框 index_input_company.send_keys(company_name)
self.driver.find_element_by_xpath('//div[contains(@class, "input-group-btn")'
' and contains(@class, "btn")'
' and contains(@class, "-hg")'
'][1]').click() # 点击搜索 #page = self.driver.page_source
#print (page) try:
company_list = self.driver.find_element_by_xpath('.//div[contains(@class, "header")][1]/a[1]') # 获取当前页面所有公司的div
href = company_list.get_attribute('href')
self.driver.get(href) # 进入公司详情页
company = self.save_company_info()
return company
except:
print('没有搜索到相应的公司')
return company_name def save_company_info(self): #a = self.driver.find_element_by_xpath('.//div[contains(@class, "nav-item-list list list-hover-show nav-line")][1]/a[1]/span[1]')
#print ('___显示手机号已是登录状态____' + a.text) company = ''
# 公司名
company_name = self.driver.find_element_by_xpath('.//div[contains(@class, "header")][1]/h1')
company += '公司名:' + company_name.text + '\n'
# 电话
phone = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[1]/div[1]/span[2]')
company += '电话:' + phone.text + '\n'
# 邮箱
email = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[1]/div[2]/span[2]')
company += 'email:' + email.text + '\n'
#e = self.driver.find_element_by_xpath('.//div[@class="detail "][1]/div[1]/div[2]/span[3]/span[1]')
# 获取更多邮箱
#email = self.get_email(e)
# 网址
try:
link = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[2]/div[1]/a[1]')
except:
link = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[2]/div[1]/span[2]')
company += '网址:' + link.text + '\n'
print(link.text)
# 地址
try:
ad = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[2]/div[2]/span[2]')
address = ad.get_attribute('title')
except:
ad = self.driver.find_element_by_xpath('.//div[contains(@class, "detail ")][1]/div[2]/div[2]')
address = ad.text
company += '地址:' + address + '\n'
# 简介
try:
s = self.driver.find_element_by_xpath('.//div[contains(@class, "summary")][1]/span[3]')
summary = self.get_summary(s)
except:
summary = self.driver.find_element_by_xpath('.//div[contains(@class, "summary")][1]/span[2]')
company += '简介:' + summary.text + '\n'
#法定代表人
company += '法定代表人 '
# 姓名
name = self.driver.find_element_by_xpath('.//div[contains(@class, "humancompany")][1]/div[1]/a[1]')
company += '姓名:' + name.text + '\n'
# 公司数
company_num = self.driver.find_element_by_xpath('.//div[contains(@class, "humancompany")][1]/div[2]/span[1]')
#company['company_num'] = '介绍:' + '他有' + company_num.text+'家公司分布如下'
#介绍内容 money = self.driver.find_element_by_xpath('.//div[contains(@id, "_container_baseInfo")][1]/table[1]/tbody[1]/tr[1]/td[2]/div[2]')
money_reg = money.get_attribute('title')
company += '注册资本' + money_reg + '\n' date_reg = self.driver.find_element_by_xpath('.//div[contains(@id, "_container_baseInfo")][1]/table[1]/tbody[1]/tr[2]/td[1]/div[2]/text[1]')
company += '注册时间' + date_reg.text + '\n' cancel = self.driver.find_element_by_xpath('.//div[contains(@class, "num-cancel")][1]')
company += '公司状态' + cancel.text + '\n' maincompany = self.driver.find_elements_by_xpath('.//div[contains(@class, "merge")]/div')
maincompany_info = ''
for com in maincompany:
area = com.find_element_by_xpath('.//div[contains(@class, "title")][1]')
company_detail = com.find_element_by_xpath('.//div[contains(@class, "maincompany")][1]/span[1]')
maincompany_info += area.text + company_detail.text + ','
company += maincompany_info + '\n'
#工商注册号
company_content = self.driver.find_elements_by_xpath('.//table[contains(@class, "table")'
' and contains(@class, "-striped-col")'
' and contains(@class, "-border-top-none")'
'][1]/tbody[1]/tr')
business = ''
for cont in company_content:
company_title_1 = cont.find_element_by_xpath('.//td[1]') company_info_1 = cont.find_element_by_xpath('.//td[2]')
try:
company_title_2 = cont.find_element_by_xpath('.//td[3]')
except:
pass
try:
company_info_2 = cont.find_element_by_xpath('.//td[4]')
except:
pass business += company_title_1.text + ':' + company_info_1.text + ',' + company_title_2.text + ':' + company_info_2.text + '\n' company += business + '\n'
#微信号码
try:
wechat = self.driver.find_element_by_xpath('.//div[contains(@class, "wechat")][1]/div[2]/div[2]/span[2]')
company += '微信公众号:' + wechat.text + '\n'
except:
pass print(company) return company #print(self.driver.page_source)
# 得到更多邮箱
# def get_email(self,e):
# e.click()
# email = self.driver.find_element_by_xpath('.//div[@class="body -detail modal-scroll text-center"][1]/div[1]/div')
#
# return email # 得到简介详情
def get_summary(self,e):
e.click()
summary = self.driver.find_element_by_xpath('.//div[@class="body -detail modal-scroll"][1]') return summary #得到
def get_company_name(self):
f = open('company_name.txt')
lines = f.readlines()
return lines def main(self): self.login() company_name = self.get_company_name() for cn in company_name:
name = cn.split()
#self.driver.get(self.start_url)
self.driver.get(self.start_url)
company = self.get_company_info(name[0])
if company == name[0]:
res = company + '没有相关信息' + '\n'
else:
res = company write_file = os.getcwd() + '/company.txt'
output = open(write_file, 'a')
output.write(res)
output.close()
self.driver.close() if __name__ == '__main__': # tt = GetCompanyInfo()
# tt.test()
time1 = time.time()
new_crawl = GetCompanyInfo()
new_crawl.main()
time2 = time.time()
print('用时:', int(time2-time1))
保证本地安装 geckodriver
具体代码:
链接:https://pan.baidu.com/s/14j00UXe5LRqBh0Hc3DIO1Q  密码:u42q
运行: python tianyancha.py
结果:

												

python+selenium+xpath 爬取天眼查工商基本信息的更多相关文章

  1. python应用:selenium之爬取天眼查信息

    inform_table.py # -*-coding:utf8-*- from selenium import webdriver from selenium.webdriver.common.pr ...

  2. python+selenium+PhantomJS爬取网页动态加载内容

    一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览 ...

  3. 直接请求json文件爬取天眼查企业信息(未解决验证码问题)——python3实现

    几个月前...省略一堆剧情...直接请求json文件爬取企业信息未成功,在知乎提问后,得到解决,有大佬说带上全部headers和cookie是可以的,我就又去试了下,果然可以(之前自己试的时候不行,没 ...

  4. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...

  5. python+selenium+requests爬取qq空间相册时遇到的问题及解决思路

    最近研究了下用python爬取qq空间相册的问题,遇到的问题及解决思路如下: 1.qq空间相册的访问需要qq登录并且需是好友,requests模块模拟qq登录略显麻烦,所以采用selenium的dri ...

  6. python+selenium+bs4爬取百度文库内文字 && selenium 元素可以定位到,但是无法点击问题 && pycharm多行缩进、左移

    先说一下可能用到的一些python知识 一.python中使用的是unicode编码, 而日常文本使用各类编码如:gbk utf-8 等等所以使用python进行文字读写操作时候经常会出现各种错误, ...

  7. 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)

    这里只是学习一下动态加载页面内容的抓取,并不适用于所有的页面. 使用到的工具就是python selenium和phantomjs,另外调试的时候还用了firefox的geckodriver.exe. ...

  8. 爬虫 selenium+Xpath 爬取动态js页面元素内容

    介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如 ...

  9. [python爬虫] Selenium定向爬取PubMed生物医学摘要信息

    本文主要是自己的在线代码笔记.在生物医学本体Ontology构建过程中,我使用Selenium定向爬取生物医学PubMed数据库的内容.        PubMed是一个免费的搜寻引擎,提供生物医学方 ...

随机推荐

  1. typeHandler

    package com.cainiao.aeye.chdir.manager.tddl.Handler; import com.cainiao.aeye.chdir.core.enums.AudioF ...

  2. 创建局域网yum服务器

    首先需要安装createrepo这个软件包,使用yum安装即可 创建软件库文件夹,比如mkdir -p /var/www/html/myrepo/x86_64, 将软件包拷贝到文件夹下. cd到该文件 ...

  3. idea本地调试spark,hive,kafka

    https://note.youdao.com/share/?id=753c443aa4a665679d8d00c9c50363b0&type=note#/

  4. ES6中的类和继承

    class的写法及继承 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子     function Point(x, y) {  this.x = x;  this. ...

  5. DP一下,马上出发

    简单DP i.May I ask you for a dance(体舞课软广植入) 这题的状态转移方程为:dp[i][j]=max(dp[i-1][j-1]+a[i][j],dp[i][j-1]);( ...

  6. 基于create-react-app的打包后文件路径问题

    改绝对路径为相对路径. https://segmentfault.com/q/1010000009672497直接在package.json里加 "homepage":" ...

  7. 日常安装chocolatey报错此系统上禁止运行脚本

    查看计算机上的现用执行策略get-executionpolicy  ( 默认:Restricted ) 若要在本地计算机上运行您编写的未签名脚本和来自其他用户的签名脚本,使用以下命令将计算机上的 执行 ...

  8. input[type=file]的美化

    __ 一般的选择框在美化过程中会出现各种问题,样式出错,文字无法更改等... 所有随之而生的便是这样的一种修饰方式:[将type=file的input与另一个按钮通过js绑定,这样便可以通过改变另一个 ...

  9. Kotlin 继承

    Kotlin 中所有类都继承该 Any 类,它是所有类的超类,对于没有超类型声明的类是默认超类: class Example // 从 Any 隐式继承 Any 默认提供了三个函数: equals() ...

  10. 谈谈传统BIO网络编程模型的局限性与NIO

    先来看看我们的server端: 创建一个serversocket,进行监听,每来一个客户端,就启动一个新启动为其服务: private void createListenSocket() { //如果 ...