本篇的python爬蟲會使用selenium套件去執行。
pip install selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chrome_driver_path = "path/chromedriver"
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")
driver.quit()
每個網頁都是html檔:
檢視原始碼的快捷鍵在Mac 是:⌘ + Option + U
在google搜尋欄位自動打入關鍵字
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_driver_path = ".../chromedriver"
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://google.com")
search = driver.find_element(By.NAME, "q")
search.send_keys("iphone 16")
print(driver.title)
time.sleep(10)
driver.quit()
search.send_keys(Keys.RETURN)