手機,上來自己動了這就是Appium+Python的厲害之處

作為一個男人

在最高光的時刻

就是說出那句

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

之後

還不會被人打

...

雖然在現實生活中你無法這樣

但是在這裡

就讓你體驗一番

那種呼風喚雨的感覺

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

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

使用到了一些 python 的請求庫

模擬瀏覽器的請求

我們需要抓包啥的

能不能不這樣

可不可以就寫幾行代碼

讓它自己去打開瀏覽器

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

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

等等

反正就是

老子躺著,讓它自己動

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

躺好

讓 selenium 滿足你的要求

怎麼玩呢?

那麼接下里就是

學習 python 的正確姿勢

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

什麼是 selenium ?

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

直到遇到了 python

轉身一變

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

selenium 變成了爬蟲利器

我們先來安裝一下

pip install selenium

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

小帥b用的是 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爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

接著打開 pycharm

擼點代碼

from selenium import webdriver

driver = 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()

運行一下

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

可以看到

它自己打開了 Chrome 瀏覽器

搜索了蒼老師的照片

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

這就是 selenium 的魅力

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

我們導入了 web 驅動模塊

from selenium import webdriver

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

driver = webdriver.Chrome()

有了實例之後

相當於我們有了 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()

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

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

這時候其實沒什麼鳥用

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

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

還有獲取按鈕點擊什麼的

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

可以通過 id 獲取 form 表單

login_form = driver.find_element_by_id('loginForm')

通過 name 獲取相應的輸入框

username = driver.find_element_by_name('username')

password = driver.find_element_by_name('password')

通過 xpath 獲取表單

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']")

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

input1 = driver.find_element_by_tag_name('input')

通過 class 獲取相應的元素

login = driver.find_element_by_class_name('login')

用 Chrome 瀏覽器的審核元素

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

直接 copy 就完事了

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處

如果你覺得

find_element_by_xxx_xxx

太長了

那麼你還可以這樣

driver.find_elements(By.ID, 'xxx')

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

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"

當然

我們玩的是爬蟲

要的就是源代碼

我們已經知道

通過

driver = webdriver.Chrome()

可以拿到瀏覽器對象

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

獲取請求鏈接

driver.current_url

獲取 cookies

driver.get_cookies()

獲取源代碼

driver.page_source

獲取文本的值

input.text

ok

以上就是 selenium 的常用方法

最後,想學習Python的小夥伴們!

請關注+私信回覆:“學習”就可以拿到一份我為大家準備的Python學習資料!

python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處
python爬蟲23|手機,上來自己動了這就是Appium+Python的厲害之處


分享到:


相關文章: