Google Colab 使用Selenium自動操作網頁的方法

Table of Contents

Table of Contents


本文中將會介紹Selenium的使用,並且使用Google Collaboratory(簡稱Google Colab)這個強力的Python開發工具來簡單地做Google搜尋關鍵字,把結果儲存起來。

Youtube

Selenium

官方網站:Selenium
一個能讓開發者透過程式來操作瀏覽器的程式庫。

Google Collaboratory是什麼?

可以參考本站的這篇文章Google Colab 介紹&高效秘訣

實作自動Google搜尋

導入Selenium

!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium

使用selenium

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=options)
driver.implicitly_wait(10)

操作Google搜尋網頁

首先,連接到網頁上並輸入搜尋關鍵字。

driver.get("https://google.com")

from selenium.webdriver.common.by import By

q = driver.find_element(By.NAME, 'q')
q.send_keys('cyublog')
# q.submit()

關於執行搜尋的方法,這裡介紹三種。

submit

q.submit()

KEY.ENTER

from selenium.webdriver.common.keys import Keys
driver.get("https://google.com")
q = driver.find_element(By.NAME, 'q')
q.send_keys('cyublog')
q.send_keys(Keys.ENTER)

ActionChains + Click

from selenium.webdriver.common.action_chains import ActionChains

action = ActionChains(driver)
driver.get("https://google.com")
q = driver.find_element(By.NAME, 'q')
q.send_keys('cyublog')
btnks = driver.find_elements(By.NAME, 'btnK')
for btnk in btnks:
  if btnk.is_displayed():
    action.click(btnk).perform()
    break

取得搜尋結果

gs = driver.find_elements(By.CLASS_NAME, 'g')

for g in gs:
  print(g.text)

輸出:

Web Result with Site Links

CyuBlog
https://cyublog.com
CyuBlog. This is a blog about Flutter, programing language, website and information of technology. Articles Tools · Privacy ...
Tools
Articles; Category. Android( ) · Flutter( ) · Flutter ...
Merge
ここにファイルをドロップ. または. ファイルを選択. Merge.
Articles
使用Laravel8+Vue3+Bootstrap5實作TODO List網頁應用程式的系列 ...
Python-en
This post will demo 3 Ways to save pandas data. - download as a ...
Privacy Policy for CyuBlog
The information collected by log files include internet protocol (IP ...
Dev-en
This article will show you a way how to send a notification ...
More results from cyublog.com »
CyuBlog
https://cyublog.com
CyuBlog. This is a blog about Flutter, programing language, website and information of technology. Articles Tools · Privacy ...
Cyublog / CyuBlog
https://cyublog.com.siteindices.com
a blog about Flutter, APP, Website, programming language, and information of technology.. Check cyublog valuation, traffic estimations and owner info.
Top 71 Similar websites like cyublog.com and alternatives
https://www.sitelike.org › similar › c...
· Translate this page
Top 71 Similar sites like cyublog.com. Similar Site Search. Find Similar websites like cyublog.com. cyublog.com alternatives.
About – cyu dev - Medium
https://hobrunt-dev.medium.com › about
About cyu dev on Medium. Engineer in Tokyo. Website: https://cyublog.com.
yuuto_ho - Qiita
https://qiita.com › yuuto_ho
· Translate this page
@yuuto_ho. 53Contributions · 13. Posts3. Followees2. Followers. 日々開発をしています。 CyuBlog: https://cyublog.com/. Follow. https://cyublog.com/articles/.
Cyu Cyu Blog Entries | FINAL FANTASY XIV, The Lodestone
https://na.finalfantasyxiv.com › ... › Character › Cyu Cyu
Cyu Cyu's Blog Entries. Sort by most recent, Sort by oldest, Sort by most recent comment, Sort by most comments. 1 Total. Tag. 今日も読みたい人だけで><;
Cyu's Blog Posts | Novel Updates Forum
https://forum.novelupdates.com › author › cyu.312167
Cyu's Blog Posts. Title: Replies: Rating. No blog posts have been created yet. Show Ignored Content. Novel Updates Forum.

總結

使用Selenium可以實現透過程式來操作瀏覽器的效果。
可用於網路爬蟲、自動化測試等的場合。