Python爬蟲之爬蟲神器selenium!還不會用它你就out了

我們之前在爬取某些網站的時候

使用到了一些 python 的請求庫

模擬瀏覽器的請求

我們需要抓包啥的

能不能不這樣

可不可以就寫幾行代碼

讓它自己去打開瀏覽器

自己去請求我們要爬取的網站

自己去模擬我們的一些搜索

等等

反正就是

老子躺著,讓它自己動

躺好

讓 selenium 滿足你的要求

怎麼玩呢?

那麼接下里就是

學習 python 的正確姿勢

什麼是 selenium ?

其實它就是一個自動化測試工具,支持各種主流的瀏覽器

直到遇到了 python

轉身一變

selenium 變成了爬蟲利器

我們先來安裝一下

<code>pipinstallselenium/<code>

接著我們還要下載瀏覽器驅動

這裡用的是 Chrome 瀏覽器

所以下載的是 Chrome 驅動

當然你用別的瀏覽器也闊以

去相應的地方下載就行了

Chrome:https://sites.google.com/a/chromium.org/chromedriver/downloadsEdge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/Firefox:https://github.com/mozilla/geckodriver/releasesSafari:https://webkit.org/blog/6900/webdriver-support-in-safari-10/

下載完之後

要配置一下環境變量

Python爬蟲之爬蟲神器selenium!還不會用它你就out了

接著打開 pycharm

擼點代碼

<code>from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://www.baidu.com")input= driver.find_element_by_css_selector('#kw')input.send_keys("蒼老師照片")button = driver.find_element_by_css_selector('#su')button.click()/<code>

運行一下

Python爬蟲之爬蟲神器selenium!還不會用它你就out了

可以看到

它自己打開了 Chrome 瀏覽器

搜索了蒼老師的照片

這就是 selenium 的魅力

我們來看下我們剛剛寫的代碼

我們導入了 web 驅動模塊

<code>fromseleniumimportwebdriver/<code>

接著我們創建了一個 Chrome 驅動

<code>driver= webdriver.Chrome()/<code>

有了實例之後

相當於我們有了 Chrome 瀏覽器了

<code>driver.get("https://www.baidu.com")/<code>

我們獲取到輸入框

至於怎麼獲取

等等會講

獲取到輸入框之後我們就往裡面寫入我們要搜索的內容

<code>input= driver.find_element_by_css_selector('#kw')input.send_keys("蒼老師照片")/<code>

輸入完了之後呢

我們就獲取到搜索這個按鈕

然後點擊

<code>button= driver.find_element_by_css_selector('#su')button.click()/<code>

當我們使用驅動打開了一個頁面

這時候其實沒什麼鳥用

因為我們要對那些元素進行操作

就像剛剛我們要獲取輸入框然後輸入一些內容

還有獲取按鈕點擊什麼的

selenium 提供了挺多方法給我們獲取的

當我們要在頁面中獲取一個元素的時候

可以使用這些方法

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

想要在頁面獲取多個元素呢

就可以這樣

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

比如我們打開了一個頁面

是這樣的 HTML

<code>/<code>

可以通過 id 獲取 form 表單

<code>login_form= driver.find_element_by_id('loginForm')/<code>

通過 name 獲取相應的輸入框

<code>username= driver.find_element_by_name('username')password= driver.find_element_by_name('password')/<code>

通過 xpath 獲取表單

<code>login_form= driver.find_element_by_xpath("/html/body/form[1]")login_form= driver.find_element_by_xpath("//form[1]")login_form= driver.find_element_by_xpath("//form[@id='loginForm']")/<code>

通過標籤獲取相應的輸入框

<code>input1= driver.find_element_by_tag_name('input')/<code> 

通過 class 獲取相應的元素

<code>login= driver.find_element_by_class_name('login')/<code>

用 Chrome 瀏覽器的審核元素

可以很方便獲取相應的屬性

直接 copy 就完事了

Python爬蟲之爬蟲神器selenium!還不會用它你就out了

如果你覺得

find_element_by_xxx_xxx

太長了

那麼你還可以這樣

<code>driver.find_elements(By.ID,'xxx')/<code>

By.屬性和上面的是一樣的

<code>ID="id"XPATH="xpath"LINK_TEXT="link text"PARTIAL_LINK_TEXT="partial link text"NAME="name"TAG_NAME="tag name"CLASS_NAME="class name"CSS_SELECTOR="css selector"/<code>

當然

我們玩的是爬蟲

要的就是源代碼

我們已經知道

通過

<code>driver.page_source/<code>

可以拿到瀏覽器對象

那麼要獲取源代碼還不簡單麼?

獲取請求鏈接

<code>input.text/<code>

獲取 cookies

<code>driver.get_cookies()/<code>

獲取源代碼

<code>driver.page_source/<code>

獲取文本的值

<code>input.text/<code>

ok

以上就是 selenium 的常用方法

想要了解更多相關 selenium 的可以到官方文檔查看


分享到:


相關文章: