selenium+headless chrome爬虫的实现示例

 更新时间:2021年1月9日 09:14  点击:1560

      python爬虫写起来非常快,虽然也可以用java,但是没有python来的简洁迅速

      selenium在前面总结过,是一个自动化测试库。headless chrome是无界面的浏览器模式,和PHANTOMJS类似。但是PHANTOMJS往往会出现莫名的错误,而且速度没有headless chrome快

from selenium.webdriver.chrome.options import Options
 
global DRIVER
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
  
DRIVER = webdriver.Chrome(chrome_options=chrome_options)

        爬虫的代码有一点需要注意,需要操作事件的时候最好不要直接用相应的方法,比如click。最好嵌入js脚本的方式进行调用。因为爬虫的代码执行速度很快,前端元素结构往往反应不过来,从而找出元素不可见或者不存在的错误。

province_items = DRIVER.find_element_by_class_name("city-province").find_elements_by_tag_name("a")
 
#province_item.click()
DRIVER.execute_script('arguments[0].click();',province_item)

          下面来个例子,由于做电商平台,省、市、区的数据很好找,但是没有镇、街道的信息。这里通过爬虫从淘宝网将镇,街道的信息抓取下来

#! /usr/local/bin/python
# encoding: utf-8
 
'''
Created on 2018年1月5日
 
@author: wulinfeng
@date: 2018-1-5
'''
 
import time
#import request
from selenium import webdriver
#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import pymysql
 
def init_db():
  global CONNECTION 
  CONNECTION = pymysql.connect("地址","用户名","密码","数据库" ,use_unicode=True, charset="utf8")
 
def init_web_driver():
  global DRIVER
  #DRIVER = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.2-windows\phantomjs.exe')
  #DRIVER.set_window_size(1920, 1080)
  '''  
  dcap = dict(DesiredCapabilities.PHANTOMJS)
 
  dcap["phantomjs.page.settings.userAgent"] = (
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
  )
  
  dcap["phantomjs.page.settings.viewportSize"] = (
    "width: 1920, "
    "height: 1080"
  )
  
  DRIVER = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.2-windows\phantomjs.exe',desired_capabilities=dcap)
  DRIVER.set_window_size(1920, 1080)
  '''
  
  chrome_options = Options()
  chrome_options.add_argument('--headless')
  chrome_options.add_argument('--disable-gpu')
  
  DRIVER = webdriver.Chrome(chrome_options=chrome_options)
  
  #DRIVER=webdriver.Ie()
  #DRIVER=webdriver.Chrome()
 
def close_db():
  CONNECTION.close()    
  
def close_web_driver():
  DRIVER.quit() 
  
def login_taobao(username, password):
  DRIVER.get("https://member1.taobao.com/member/fresh/deliver_address.htm?spm=a1z08.2.0.0.7dad47611Wnj46")
  #DRIVER.get("https://login.taobao.com/member/login.jhtml?spm=a21bo.2017.201864-2.d1.7d2082a4FxukGr&f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F")
  #选择登陆方式
  DRIVER.find_element_by_xpath("//*[@id=\"J_Quick2Static\"]").click()
 
  #登陆
  input_user = DRIVER.find_element_by_xpath("//*[@id=\"TPL_username_1\"]")
  input_user.clear()
  input_user.send_keys(username)
  
  DRIVER.find_element_by_xpath("//*[@id=\"TPL_password_1\"]").send_keys(password)
  DRIVER.find_element_by_xpath("//*[@id=\"J_SubmitStatic\"]").click();
  
  time.sleep(0.5)
  
def get_data():
  #点击地址选择
  #DRIVER.find_element_by_xpath("//*[@id=\"city-title\"]").click()
  city_title = DRIVER.find_element_by_id("city-title")
  DRIVER.execute_script('arguments[0].click();',city_title)
  
  get_province_and_sub()
  
def get_province_and_sub():
  #获得省列表
  province_items = DRIVER.find_element_by_class_name("city-province").find_elements_by_tag_name("a")
  
  for province_item in province_items:
    pid = province_item.get_attribute("attr-id");
    pname = province_item.get_attribute("title");
    if pid == "-1":
      print("continue province")
      continue
    
    sql = "insert into region_province_t (province_id,province) values('"+pid+"','"+pname+"')"
    print(sql) 
    cursor = CONNECTION.cursor()
    cursor.execute(sql)
    CONNECTION.commit()
    
    #province_item.click()
    DRIVER.execute_script('arguments[0].click();',province_item)
    time.sleep(0.5)
    
    get_city_and_sub(pid)
    back_tab(0) 
  
def get_city_and_sub(pid):
  #获得市列表
  city_items = DRIVER.find_element_by_class_name("city-city").find_elements_by_tag_name("a")
  for city_item in city_items:
    cid = city_item.get_attribute("attr-id");
    cname = city_item.get_attribute("title");
    if cid == "-1":
      print("continue city")
      continue
  
    sql = "insert into region_city_t (city_id,city,province_id) values('"+cid+"','"+cname+"','"+pid+"')"
    print(sql) 
    cursor = CONNECTION.cursor()
    cursor.execute(sql)
    CONNECTION.commit()
    
    #city_item.click()
    DRIVER.execute_script('arguments[0].click();',city_item)
    time.sleep(1)
    
    get_area_and_sub(cid)
    back_tab(1)
    
def get_area_and_sub(cid):
  #获得县区列表
  area_items = DRIVER.find_element_by_class_name("city-district").find_elements_by_tag_name("a")
  for area_item in area_items:
    aid = area_item.get_attribute("attr-id");
    aname = area_item.get_attribute("title");
    if aid == "-1":
      print("continue area")
      continue
    
    sql = "insert into region_area_t (area_id,area,city_id) values('"+aid+"','"+aname+"','"+cid+"')"
    print(sql) 
    cursor = CONNECTION.cursor()
    cursor.execute(sql)
    CONNECTION.commit()
    
    #area_item.click()
    DRIVER.execute_script('arguments[0].click();',area_item)
    time.sleep(0.5)
    
    get_town_and_sub(aid)
    back_tab(2)
    
  
def get_town_and_sub(aid):
  #获得镇列表
  town_items = DRIVER.find_element_by_class_name("city-street").find_elements_by_tag_name("a")
  for town_item in town_items:
    tid = town_item.get_attribute("attr-id");
    tname = town_item.get_attribute("title");
    if tid == "-1":
      print("continue town")
      continue
    
    sql = "insert into region_town_t (town_id,town,area_id) values('"+tid+"','"+tname+"','"+aid+"')"
    print(sql) 
    cursor = CONNECTION.cursor()
    cursor.execute(sql)
    CONNECTION.commit()
    
def back_tab(index):
  districtEle = DRIVER.find_element_by_class_name("city-select-tab").find_elements_by_tag_name("a")[index]
  DRIVER.execute_script('arguments[0].click();',districtEle)
  time.sleep(0.5)
  
init_db()
init_web_driver()
login_taobao("用户名", "密码")
get_data()
close_db()
close_web_driver()

到此这篇关于selenium+headless chrome爬虫的实现示例的文章就介绍到这了,更多相关selenium+headless chrome爬虫内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞! 

[!--infotagslink--]

相关文章

  • 用C#做网络爬虫的步骤教学

    在本篇内容里小编给大家分享的是关于用C#做网络爬虫的步骤和方法,需要的朋友们可以参考下。...2020-06-25
  • Python爬虫基础之selenium库的用法总结

    今天带大家来学习selenium库的使用方法及相关知识总结,文中非常详细的介绍了selenium库,对正在学习python的小伙伴很有帮助,需要的朋友可以参考下...2021-05-25
  • 利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)

    这篇文章主要介绍了利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化),本文给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-24
  • selenium 反爬虫之跳过淘宝滑块验证功能的实现代码

    这篇文章主要介绍了selenium 反爬虫之跳过淘宝滑块验证功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-27
  • 从chrome调试工具中把拖延时间的东西找出来

    我打开android开发手册的时候:http://www.csdn123.com/html/android/reference/packages.html 发现打开速度很慢,我用按了一下F12打开调试面板,切换到网络的选项卡network...2016-05-19
  • 利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...2020-06-25
  • Nginx中配置过滤爬虫的User-Agent的简单方法

    这篇文章主要介绍了Nginx中配置过滤爬虫的User-Agent的简单方法,文中罗列了一些常用搜索引擎的爬虫名称以免造成不必要的过滤,需要的朋友可以参考下...2016-01-27
  • python爬虫用request库处理cookie的实例讲解

    在本篇内容里小编给大家整理的是一篇关于python爬虫用request库处理cookie的实例讲解内容,有需要的朋友们可以学习参考下。...2021-02-21
  • Python爬虫之Selenium实现关闭浏览器

    这篇文章主要介绍了Python爬虫之Selenium实现关闭浏览器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-04
  • python 爬取京东指定商品评论并进行情感分析

    本文主要讲述了利用Python网络爬虫对指定京东商城中指定商品下的用户评论进行爬取,对数据预处理操作后进行文本情感分析,感兴趣的朋友可以了解下...2021-05-28
  • Chrome插件开发系列一:弹窗终结者开发实战

    从这一节开始,我们将从零开始打造我们的chrome插件工具库,第一节我们将讲一下插件开发的基础知识并构建一个简单但却很实用的插件,在构建之前,我们先简单的了解一下插件以及插件开发的基础知识...2020-10-03
  • 基于C#实现网页爬虫

    这篇文章主要为大家详细介绍了基于C#实现网页爬虫的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • c# Selenium爬取数据时防止webdriver封爬虫的方法

    这篇文章主要介绍了c# Selenium爬取数据时防止webdriver封爬虫的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-15
  • C#爬虫通过代理刷文章浏览量

    本篇文章给大家分享了C#爬虫通过代理刷文章浏览量的详细步骤和方法,有兴趣的朋友可以参考学习下。...2020-06-25
  • node.js爬虫框架node-crawler初体验

    这篇文章主要介绍了node.js爬虫框架node-crawler的相关资料,帮助大家利用node.js进行爬虫,感兴趣的朋友可以了解下...2020-10-29
  • 快速一键生成Python爬虫请求头

    这篇文章主要介绍了如何快速一键生成Python爬虫请求头,帮助大家更好的理解和学习使用python爬虫,感兴趣的朋友可以了解下...2021-03-05
  • python网络爬虫实现发送短信验证码的方法

    这篇文章主要介绍了python网络爬虫实现发送短信验证码的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
  • python自动化测试selenium执行js脚本实现示例

    这篇文章主要为大家介绍了python自动化测试selenium执行js脚本的实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...2021-11-13
  • Python Selenium操作Cookie的实例方法

    在本篇文章里小编给大家整理的是一篇关于Python Selenium操作Cookie的实例方法,有需要的朋友们可以学习参考下。...2021-02-28
  • python编程使用selenium编写测试用例

    这篇文章主要为大家介绍了在python编程学习中如何使用selenium来编写测试用例,文中给出了详细的测试用例代码,有需要的朋友可以借鉴参考下...2021-10-09