1. # -*- coding:utf-8 -*-
    # author: kevin
    # CreateTime: 2018/8/16
    # software-version: python 3.7
  2.  
  3. import time
    from selenium import webdriver
    from selenium.webdriver import Firefox
    import os
  4.  
  5. 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)
  6.  
  7. self.start_url = 'https://www.tianyancha.com'
  8.  
  9. def login(self):
    """
    登录并检查状态
    :return:
    """
    # try:
    self.driver.get(self.start_url)
  10.  
  11. #print(self.driver.get_cookies())
  12.  
  13. 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)
  14.  
  15. if is_login != -1:
    print('登录成功')
  16.  
  17. 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)
  18.  
  19. 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
  20.  
  21. 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
  22.  
  23. 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')
  24.  
  25. 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('')
  26.  
  27. 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]')
  28.  
  29. #print(login_button.text)
    time.sleep(1)
    login_button.click()
  30.  
  31. def get_company_info(self, company_name):
    """
    获取想要的公司信息
    :param company_name:
    :param company_onwer:
    :return:
    """
    time.sleep(1)
    page = self.driver.page_source
  32.  
  33. index_input_company = self.driver.find_element_by_xpath('//input[@id="home-main-search"]') # 主页搜索框
  34.  
  35. 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() # 点击搜索
  36.  
  37. #page = self.driver.page_source
    #print (page)
  38.  
  39. 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
  40.  
  41. def save_company_info(self):
  42.  
  43. #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)
  44.  
  45. 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+'家公司分布如下'
    #介绍内容
  46.  
  47. 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'
  48.  
  49. 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'
  50.  
  51. cancel = self.driver.find_element_by_xpath('.//div[contains(@class, "num-cancel")][1]')
    company += '公司状态' + cancel.text + '\n'
  52.  
  53. 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]')
  54.  
  55. 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
  56.  
  57. business += company_title_1.text + ':' + company_info_1.text + ',' + company_title_2.text + ':' + company_info_2.text + '\n'
  58.  
  59. 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
  60.  
  61. print(company)
  62.  
  63. return company
  64.  
  65. #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
  66.  
  67. # 得到简介详情
    def get_summary(self,e):
    e.click()
    summary = self.driver.find_element_by_xpath('.//div[@class="body -detail modal-scroll"][1]')
  68.  
  69. return summary
  70.  
  71. #得到
    def get_company_name(self):
    f = open('company_name.txt')
    lines = f.readlines()
    return lines
  72.  
  73. def main(self):
  74.  
  75. self.login()
  76.  
  77. company_name = self.get_company_name()
  78.  
  79. 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
  80.  
  81. write_file = os.getcwd() + '/company.txt'
    output = open(write_file, 'a')
    output.write(res)
    output.close()
    self.driver.close()
  82.  
  83. if __name__ == '__main__':
  84.  
  85. # 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
    结果:

  1.  

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. 五一培训 DAY1

    DAY1 枚举 例题1 题解: 例题2 题解: 例题3 题解: vis[ ]判断是否为素数,pri[ ]储存素数 例题4 题解: 例题5 题解: PS: i  <  1<<n    ...

  2. js encode方法

    js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1 ...

  3. Linux下使用acme.sh 配置https 免费证书

    acme.sh 简单来说acme.sh 实现了 acme 协议, 可以从 let‘s encrypt 生成免费的证书.acme.sh 有以下特点:一个纯粹用Shell(Unix shell)语言编写的 ...

  4. varnish 相关说明

    一.关于Varnish Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能 ...

  5. oracle 审计功能

    Oracle 11g推出了审计功能,但这个功能会针对很多操作都产生审计文件.aud,日积月累下来这些文件也很多,默认情况下,系统为了节省资源,减少I/0操作,其审计功能是关闭的 一.审计功能关闭 1. ...

  6. 工作中常用的 Shell 命令及技巧

    调试 bash 脚本的技巧 加 -x 参数运行 bash 脚本时,会显示执行的语句 # 也可以在 demo.sh 中加上 set -x bash -x demo.sh 设置环境变量,然后通过如上方式运 ...

  7. [524.A]2019-05-02(星期四)登顶梧桐山邀请

    *** 看房活动召集 ***五一期间天气炎热, 除了登山活动, 还将组织看房活动.拥有一套自己的住房是很多深圳人的梦想.政府十三五期间计划供应人才住房和保障性住房35万套, 与需求相比仍很少, 排队的 ...

  8. WindowsService(Windows服务)开发步骤

    https://www.cnblogs.com/moretry/p/4149489.html

  9. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  10. Shiro 与spring 整合的及简单使用(转)

    文章完全转载自: http://www.cnblogs.com/learnhow/p/5694876.html  ,大家可以点击访问原文链接,个人仅作学习收藏 ! 本篇内容大多总结自张开涛的<跟 ...