Python 和Selenium进行航班价格检查
网络爬虫一直是一种从网站上提取数据的有用技术,包括用于航空票价检查。在本文中,我们将探讨如何使用Selenium构建一个航班价格检查程序,Selenium是一种流行的web测试自动化工具。利用Selenium的功能,我们可以自动化收集和比较不同航空公司的航班价格,为用户节省时间和精力。
设置
Firefox可执行文件
- 从这里下载Firefox浏览器安装程序
-
下载完成后,安装浏览器,会自动将一个exe文件放置在 C:\ Program Files \ Mozilla Firefox \ firefox.exe 。我们稍后会用到它。
Gecko驱动程序
-
Windows用户可以从这里下载gecko驱动程序。其他版本请见releases。
-
解压缩并将“geckodriver.exe”文件放置在C:\目录中。我们稍后在代码中会引用它。
Selenium Python包
我们将使用Selenium Webdriver的最新版本,所以使用pip安装以下内容-
pip3 install -U selenium
pip3 install -U webdriver-manager
步骤
- 导入必要的库 – Selenium 和 time
-
设置 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的 options ,并将Firefox的二进制位置设置为 C:\Program Files\Mozilla Firefox\firefox.exe 。
-
然后,使用 webdriver.Firefox()函数创建一个Firefox的webdriver实例,传入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是一个强大的网络自动化和抓取工具,可用于在没有API的情况下从网站收集信息。Python的灵活性、易用性和强大的工具生态系统使其成为最适合进行抓取的语言。这个脚本展示了如何使用只有几行代码就可以自动化浏览器活动并从网页上检索数据。