欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

selenium+headless chrome爬蟲的實現(xiàn)示例

 更新時間:2021年01月08日 17:00:00   作者:guduyishuai  
這篇文章主要介紹了selenium+headless chrome爬蟲的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

      python爬蟲寫起來非??欤m然也可以用java,但是沒有python來的簡潔迅速

      selenium在前面總結(jié)過,是一個自動化測試庫。headless chrome是無界面的瀏覽器模式,和PHANTOMJS類似。但是PHANTOMJS往往會出現(xiàn)莫名的錯誤,而且速度沒有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)

        爬蟲的代碼有一點需要注意,需要操作事件的時候最好不要直接用相應(yīng)的方法,比如click。最好嵌入js腳本的方式進行調(diào)用。因為爬蟲的代碼執(zhí)行速度很快,前端元素結(jié)構(gòu)往往反應(yīng)不過來,從而找出元素不可見或者不存在的錯誤。

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)

          下面來個例子,由于做電商平臺,省、市、區(qū)的數(shù)據(jù)很好找,但是沒有鎮(zhèn)、街道的信息。這里通過爬蟲從淘寶網(wǎng)將鎮(zhèn),街道的信息抓取下來

#! /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("地址","用戶名","密碼","數(shù)據(jù)庫" ,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("http://*[@id=\"J_Quick2Static\"]").click()
 
  #登陸
  input_user = DRIVER.find_element_by_xpath("http://*[@id=\"TPL_username_1\"]")
  input_user.clear()
  input_user.send_keys(username)
  
  DRIVER.find_element_by_xpath("http://*[@id=\"TPL_password_1\"]").send_keys(password)
  DRIVER.find_element_by_xpath("http://*[@id=\"J_SubmitStatic\"]").click();
  
  time.sleep(0.5)
  
def get_data():
  #點擊地址選擇
  #DRIVER.find_element_by_xpath("http://*[@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):
  #獲得縣區(qū)列表
  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):
  #獲得鎮(zhèn)列表
  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()

到此這篇關(guān)于selenium+headless chrome爬蟲的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)selenium+headless chrome爬蟲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Python 20行簡單實現(xiàn)有道在線翻譯的詳解

    Python 20行簡單實現(xiàn)有道在線翻譯的詳解

    這篇文章主要介紹了Python實現(xiàn)有道在線翻譯的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng)

    Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python學(xué)習(xí)筆記之集合的概念和簡單使用示例

    Python學(xué)習(xí)筆記之集合的概念和簡單使用示例

    這篇文章主要介紹了Python學(xué)習(xí)筆記之集合的概念和簡單使用,涉及Python集合的定義、查找、添加、刪除等相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2019-08-08
  • python基礎(chǔ)之while循環(huán)、for循環(huán)詳解及舉例

    python基礎(chǔ)之while循環(huán)、for循環(huán)詳解及舉例

    所謂循環(huán)結(jié)構(gòu)就是程序中控制某條或某些指令重復(fù)執(zhí)行的結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)之while循環(huán)、for循環(huán)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Python3 pickle模塊的使用方法詳細介紹

    Python3 pickle模塊的使用方法詳細介紹

    pickle提供了一個簡單的持久化功能??梢詫ο笠晕募男问酱娣旁诖疟P上,pickle序列化后的數(shù)據(jù),可讀性差,人一般無法識別,本文詳細介紹了pickle模塊的使用方法
    2021-10-10
  • Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實現(xiàn)

    Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實現(xiàn)

    這篇文章主要介紹了Pandas 重塑(stack)和軸向旋轉(zhuǎn)(pivot)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python填充任意顏色,不同算法時間差異分析說明

    Python填充任意顏色,不同算法時間差異分析說明

    這篇文章主要介紹了Python填充任意顏色,不同算法時間差異分析說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Flask實現(xiàn)圖片的上傳、下載及展示示例代碼

    Flask實現(xiàn)圖片的上傳、下載及展示示例代碼

    這篇文章主要介紹了Flask實現(xiàn)圖片的上傳、下載及展示示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Python多分支if語句的使用

    Python多分支if語句的使用

    這篇文章主要介紹了Python多分支if語句的使用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 給 TensorFlow 變量進行賦值的方式

    給 TensorFlow 變量進行賦值的方式

    今天小編就為大家分享一篇給 TensorFlow 變量進行賦值的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評論