使用 Python 和 Selenium 的航班價格檢查器


網路爬取一直以來都是一種從網站提取資料的有用技術,用於各種目的,包括航空機票的價格檢查。在本文中,我們將探討如何使用 Selenium(一種流行的網頁測試自動化工具)構建航班價格檢查器。透過利用 Selenium 的功能,我們可以自動化收集和比較不同航空公司航班價格的過程,為使用者節省時間和精力。

設定

Firefox 可執行檔案

  • 這裡下載 Firefox 瀏覽器安裝程式

  • 下載完成後,安裝瀏覽器,一個 exe 檔案將自動放置在C:\Program Files\Mozilla Firefox\firefox.exe中。我們稍後會用到它。

Gecko 驅動程式

  • Windows 使用者可以從這裡下載 gecko 驅動程式。對於其他版本,請參閱發行說明

  • 解壓 zip 檔案並將“geckodriver.exe”檔案放置在 C:\ 目錄中。我們稍後將在程式碼中引用它。

Selenium Python 包

我們將使用最新版本的 Selenium Webdriver,因此使用 pip 安裝以下內容:

pip3 install -U selenium
pip3 install -U webdriver-manager

演算法

  • 匯入必要的庫 - Seleniumtime

  • 設定Firefox Gecko驅動程式路徑

  • 開啟要抓取的網站

  • 識別要抓取的必要元素

  • 輸入出發和到達地點以及出發和返回日期

  • 點選搜尋按鈕

  • 等待搜尋結果載入

  • 抓取不同航空公司的價格

  • 將資料儲存為易於閱讀和分析的格式

  • 比較價格並確定最便宜的選擇

示例

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import time

# Set Firefox options
firefox_options = Options()
firefox_options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'

# Initialize webdriver with Firefox
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', options=firefox_options)

# Set URL and date of travel
url = 'https://paytm.com/flights/flightSearch/BBI-Bhubaneshwar/DEL-Delhi/1/0/0/E/2023-04-22'
date_of_travel = "2023-04-22"

# Print URL
print(f"URL: {url}")

# Load webpage
driver.get(url)

# Wait for 5 seconds
time.sleep(5)

# Find search button and click it
search_button = driver.find_element(By.CLASS_NAME, "_3LRd")
search_button.click()

# Find all elements with class name '_2gMo'
prices_elements = driver.find_elements(By.CLASS_NAME, "_2gMo")

# Get text of all elements
prices_text = [price.text for price in prices_elements]

# Convert text to integers
prices = [int(p.replace(',', '')) for p in prices_text]

# Display the minimum airfare price
print(f"Minimum Airfare Price: {min(prices)}")

# Display all prices
print(f"All prices:\n {prices}")

輸出

Minimum Airfare Price: 4471
All prices:
 [4471, 4472, 4544, 4544, 4679, 4838, 5497, 5497, 5866, 6991, 7969, 8393, 8393, 8393, 8393, 8393, 8445, 8445, 8445, 8445, 8445, 8498, 8498, 8498, 8540, 8898, 8898, 8898, 8898, 8898, 9203, 9207, 9385, 10396, 10554, 10896, 11390, 11433, 11766, 11838, 11838, 11838, 12518, 12678, 12678, 12678, 12735, 12735, 12735, 12735, 12767, 12767, 12787, 12787, 12787, 12787, 12840, 12945, 12966, 12981, 13069, 13145, 13145, 13145, 13145, 13152, 13525, 13537, 13537, 13571, 13610, 13633, 13828, 13956, 14358, 14630, 14630, 14828, 14838, 15198, 15528, 15849, 15954, 16479, 17748, 17748, 18506, 20818, 20818, 20818, 20818, 21992, 23590, 24468, 25483, 25483, 26628, 75271]

解釋

  • 首先,匯入必要的庫:來自 selenium 的 webdriver 和 Options,來自selenium.webdriver.common.by的 By,以及time

  • 接下來,使用 Options() 設定 Firefox 選項,並將 Firefox 的二進位制檔案位置設定為C:\Program Files\Mozilla Firefox\firefox.exe

  • 然後使用 Firefox 建立一個 webdriver 例項,使用webdriver.Firefox() 函式,傳入 Gecko 驅動程式可執行檔案的路徑和 Firefox 選項。

  • 將 URL 和旅行日期分別設定為https://paytm.com/flights/flightSearch/BBI-Bhubaneshwar/DEL-Delhi/1/0/0/E/2023-04-22 和“2023-04-22”。然後將 URL 列印到控制檯。

  • 使用driver.get(url)將網頁載入到瀏覽器中。

  • 然後指令碼使用time.sleep(5)等待 5 秒。

  • 使用driver.find_element(By.CLASS_NAME, "_3LRd")找到網頁上的搜尋按鈕,並將其儲存在 search_button 變數中。然後在 search_button 變數上呼叫 click() 方法來模擬點選按鈕。

  • 使用driver.find_elements(By.CLASS_NAME, "_2gMo")找到網頁上所有具有類名 _2gMo 的元素,並將它們儲存在 prices_elements 列表中。

  • 使用列表推導式提取 prices_elements 列表中所有元素的文字,並將結果儲存在prices_text列表中。

  • 使用 replace() 方法從 prices_text 中的每個元素中刪除逗號,並將結果字串轉換為整數,使用 int()。這是使用另一個列表推導式完成的,生成的整數列表儲存在 prices 列表中。

  • 使用min() 函式找到 prices 中的最小值,並將其列印到控制檯。

  • 最後,將 prices 中的所有值列印到控制檯。

應用

使用 Python 和 Selenium,此程式碼可用於開始從 Paytm 的航班搜尋網站抓取機票價格,並且您可以根據需要修改它以滿足特定需求,以及新增其他功能,例如將抓取的資料儲存在檔案中,以及傳送包含價格的電子郵件通知等。

結論

Selenium 是一種強大的 Web 自動化和抓取工具,可用於在沒有 API 的情況下從網站收集資訊。Python 的多功能性、易用性和強大的工具生態系統使其成為抓取的完美語言。此指令碼展示瞭如何僅用幾行程式碼即可自動化瀏覽器活動並從網頁檢索資料。

更新於:2023-08-21

634 次檢視

開啟您的職業生涯

透過完成課程獲得認證

立即開始
廣告