自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python爬蟲很強大,在爬蟲里如何自動操控瀏覽器呢?

開發(fā) 后端
python通過selenium爬取數(shù)據(jù)是很多突破封鎖的有效途徑。但在使用selenium中會遇到很多問題,本文就通過一問一答的形式來通熟易懂的普及如何通過selenium執(zhí)行javascript程序,進而獲取動態(tài)執(zhí)行后的網(wǎng)頁。如果你喜歡,歡迎轉(zhuǎn)發(fā)本文。

 概述:

python通過selenium爬取數(shù)據(jù)是很多突破封鎖的有效途徑。但在使用selenium中會遇到很多問題,本文就通過一問一答的形式來通熟易懂的普及如何通過selenium執(zhí)行javascript程序,進而獲取動態(tài)執(zhí)行后的網(wǎng)頁。如果你喜歡,歡迎轉(zhuǎn)發(fā)本文。

[[272405]]

python爬蟲編程:用selenium執(zhí)行javascript出錯了,該咋改?

問題:

小明開始學(xué)習(xí)python爬蟲編程了,仿佛整個互聯(lián)網(wǎng)的數(shù)據(jù)都快被他納入囊中了。今天,他又試圖完成一個高難度動作,他想讓selenium中抓取到以下HTML后,并自動執(zhí)行js腳本,模仿鼠標(biāo)自動執(zhí)行一個點擊動作。但令他很失望的是,居然,居然,沒用!

  1. <div class="vbseo_liked"
  2. <a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> 
  3. <a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollow">Sikonge</a> 
  4. <a href="http://www.jamiiforums.com/member.php?u=8979" rel="nofollow">Ab-Titchaz</a> 
  5. and 
  6. <a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a> 
  7. like this. 
  8. </div> 

這是他執(zhí)行的代碼。

  1. browser.execute_script("document.getElement(By.xpath(\"//div[@class='vbseo_liked']/a[contains(@onclick, 'return vbseoui.others_click(this)')]\").click()"

它沒用,沒有反應(yīng)。究竟做錯了什么?

Python大大的答案:

要點回答:

使用selenium查找元素并將其傳遞execute_script()給單擊:

  1. link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]'
  2. browser.execute_script('arguments[0].click();', link) 

如果要從頭解決這問題,那么以下就是需要了解它的一系列事情:

  • 如何使用JavaScript模擬點擊?

這就是我做的東西。這很簡單,但它有效:

  1. function eventFire(el, etype){ 
  2.  if (el.fireEvent) { 
  3.  el.fireEvent('on' + etype); 
  4.  } else { 
  5.  var evObj = document.createEvent('Events'); 
  6.  evObj.initEvent(etype, truefalse); 
  7.  el.dispatchEvent(evObj); 
  8.  } 

用法:

  1. eventFire(document.getElementById('mytest1'), 'click'); 
  • 如何在Python里進行模擬點擊呢?首先制定一個自定義的預(yù)期條件,等待元素被“執(zhí)行”:
  1. class wait_for_text_not_to_end_with(object): 
  2.  def __init__(self, locator, text): 
  3.  self.locator = locator 
  4.  self.text = text 
  5.  def __call__(self, driver): 
  6.  try : 
  7.  element_text = EC._find_element(driver, self.locator).text.strip() 
  8.  return not element_text.endswith(self.text) 
  9.  except StaleElementReferenceException: 
  10.  return False 

定義完畢后,如何在程序里調(diào)用這個類呢?看看以下代碼:

  1. from selenium import webdriver 
  2. from selenium.common.exceptions import StaleElementReferenceException 
  3. from selenium.webdriver.common.by import By 
  4. from selenium.webdriver.support.ui import WebDriverWait 
  5. from selenium.webdriver.support import expected_conditions as EC 
  6. class wait_for_text_not_to_end_with(object): 
  7.  def __init__(self, locator, text): 
  8.  self.locator = locator 
  9.  self.text = text 
  10.  def __call__(self, driver): 
  11.  try : 
  12.  element_text = EC._find_element(driver, self.locator).text.strip() 
  13.  return not element_text.endswith(self.text) 
  14.  except StaleElementReferenceException: 
  15.  return False 
  16. browser = webdriver.PhantomJS() 
  17. browser.maximize_window() 
  18. browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html"
  19. username = browser.find_element_by_id("navbar_username"
  20. password = browser.find_element_by_name("vb_login_password_hint"
  21. username.send_keys("MarioP"
  22. password.send_keys("codeswitching"
  23. browser.find_element_by_class_name("loginbutton").click() 
  24. wait = WebDriverWait(browser, 30) 
  25. wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]'))) 
  26. wait.until(EC.title_contains('Kenyan & Tanzanian')) 
  27. wait.until(EC.visibility_of_element_located((By.ID, 'postlist'))) 
  28. # click "11 others" link 
  29. link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]'
  30. link.click() 
  31. browser.execute_script(""
  32. function eventFire(el, etype){ 
  33.  if (el.fireEvent) { 
  34.  el.fireEvent('on' + etype); 
  35.  } else { 
  36.  var evObj = document.createEvent('Events'); 
  37.  evObj.initEvent(etype, truefalse); 
  38.  el.dispatchEvent(evObj); 
  39.  } 
  40. eventFire(arguments[0], "click"); 
  41. """, link) 
  42. # wait for the "div" not to end with "11 others link this." 
  43. wait.until(wait_for_text_not_to_end_with((By.CLASS_NAME, 'vbseo_liked'), "11 others like this.")) 
  44. print 'success!!' 
  45. browser.close() 

看,如何在python里通過selenium來爬取數(shù)據(jù)就是這么簡單。要點掌握好,開始編制自己的爬蟲吧。

責(zé)任編輯:華軒 來源: 百家號
相關(guān)推薦

2023-06-29 13:09:36

2024-03-08 12:17:39

網(wǎng)絡(luò)爬蟲Python開發(fā)

2017-10-26 15:17:06

Python爬蟲框架歌曲下載

2021-12-15 22:04:11

瀏覽器重復(fù)登錄

2019-08-20 10:16:46

PythonChrome插件

2024-08-27 09:36:34

2024-08-21 15:27:28

2021-02-15 10:32:06

C#Selenium網(wǎng)頁

2022-04-07 09:00:00

跨瀏覽器測試自動化服務(wù)異常

2021-04-01 10:51:44

機器人人工智能編程

2023-06-28 00:05:44

人工智能聊天機器人ChatGPT

2022-04-25 10:26:11

Python代碼瀏覽器

2011-10-13 09:42:24

IE

2018-02-23 14:30:13

2013-11-18 14:42:53

瀏覽器渲染

2020-08-02 15:24:13

Chrome瀏覽器Windows 10

2012-03-20 11:07:08

2017-08-09 15:27:33

python爬蟲開發(fā)工具

2012-03-20 11:41:18

海豚瀏覽器

2012-03-20 11:31:58

移動瀏覽器
點贊
收藏

51CTO技術(shù)棧公眾號