廉价raid_如何查找80行代码中的廉价航班
廉價raid
Since travelling has become practically impossible, I decided to think about how I could start preparing for my next trip. That’s right, I got travel on my mind.
由于實際上已經不可能旅行了,所以我決定考慮如何才能為下次旅行做準備。 沒錯,我想到了旅行。
As most people know, plane tickets prices vary a lot. That is because airline companies use yield management, a pricing strategy that aims to anticipate consumer behaviour to maximize profits.
眾所周知,機票價格相差很大。 那是因為航空公司使用收益管理,這是一種定價策略,旨在預期消費者的行為以最大化利潤。
For instance, with the code I will present below, I looked at a plane ticket Montreal-Oslo 48 times per day for 2 days. The price varied between 891$ and 1298$ (all data was scrapped from Kayak, right here). Same dates, same destinations, a huge difference in prices.
例如,使用下面將要顯示的代碼,我每天兩次瀏覽蒙特利爾-奧斯陸飛機票,共2天。 價格在891美元和1298美元之間變化 (所有數據都是從Kayak 此處的廢棄的)。 相同的日期,相同的目的地,價格差異巨大。
W
w ^
Here is the code.
這是代碼。
包和參數 (Packages and parameters)
Let’s begin by importing the necessary packages:
讓我們從導入必要的包開始:
# import librariesfrom bs4 import BeautifulSoupfrom selenium import webdriver
import pandas as pd
import datetime, re, time, itertools, smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
Then, we define the parameters needed for the code to work:
然后,我們定義代碼運行所需的參數:
#Define parametersstart_date = datetime.datetime(2020,9,13)days_range = 2
trip_length, min_length, max_length = 7, 6, 12
price_goal = 900
cities = 'YMQ-OSL'
Start_date: the earliest date for the flight to take off.
Start_date :航班起飛的最早日期。
days_range: The maximum number of days away from the start_date to depart. Basically, what is the latest the trip could start (so September 15th here).
days_range :距出發start_date要離開的最大天數。 基本上,這次旅行可以開始的最新時間是什么(所以這里是9月15日)。
trip_length, min_length, max_length : the desired trip length, as well as the minimum and maximum acceptable lengths. Can be equal to trip_length.
trip_length, min_length, max_length :所需的行程長度以及最小和最大可接受長度。 可以等于trip_length 。
price_goal: Price at which you want to receive an email alert.
price_goal :您要接收電子郵件警報的價格。
cities: The cities for the departure and arrival. In order to find the correct abbreviation, look at the URL when you search for flights on the website.
cities :出發和到達的城市。 為了找到正確的縮寫,請在網站上搜索航班時查看URL。
創建日期組合 (Creating dates combinations)
Now that all the parameters are defined, here is how to create all the possible departing/returning dates combinations for the trip.
既然已經定義了所有參數,下面就是如何為行程創建所有可能的出發/回程日期組合。
Indeed, it’s possible that you have flexible dates, both in departure/return or in lenght of the trip. The following code will generate, based on the parameters defined earlier, all the possible travelling dates.
確實,您可能有靈活的日期,無論是出發/回程還是行程時間。 以下代碼將根據先前定義的參數生成所有可能的旅行日期。
First, I used the itertools library to generate all possible combinations of dates.
首先,我使用了itertools庫來生成所有可能的日期組合。
Then, the following loop allows us to get rid of all the dates that don’t respect the conditions (minimum/maximum length of the trip) or incoherent dates (return before departure date).
然后,下面的循環使我們能夠擺脫所有不符合條件的日期(行程的最小/最大長度)或不連貫的日期(出發日期之前返回)。
Finally, the following lines of code clean the dates to make them usable for the loop that follows.
最后,以下代碼行清除了日期,以使其可用于隨后的循環。
#Cleaning dates to make them usable in loopfor i in range(len(departing)):departing[i]=re.sub(' 00:00:00', '', departing[i])
returning[i]=re.sub(' 00:00:00', '', returning[i])
收集數據并接收警報 (Scraping the data and receiving alerts)
Now, on to the real stuff. Here is the algorithm used to find the best flights and ensure that you receive an alert when the prices are low enough.
現在,介紹真正的東西。 這是用于查找最佳航班并確保您在價格足夠低時收到警報的算法。
If the price found is inferior to the price_goal parameter, send an email alert, containing the URL where the price was found.
如果找到的價格低于price_goal參數,請發送電子郵件警報,其中包含找到價格的URL。
Here is the full code.
這是完整的代碼。
There are 3 things I want to mention here. First, don’t forget to download a chrome driver (and adjust the executable path in line 6) to use Selenium.
我想在這里提到三件事。 首先,不要忘記下載chrome驅動程序(并在第6行中調整可執行路徑)以使用Selenium。
Second, don’t forget to update the sender, password and receiver parameters. Those specify what address is sending the email and what address will be receiving it. You could also change the email content (the subject or the body) if you want.
其次,不要忘記更新發送者,密碼和接收者參數。 那些指定發送電子郵件的地址和接收電子郵件的地址。 如果需要,您還可以更改電子郵件內容(主題或正文)。
Third, the code is currently set up for a Gmail address sender. For any other type of address, the port (line 22) and the stmp.gmail.com (line 37) should be changed.
第三,目前已為Gmail地址發件人設置了代碼。 對于任何其他類型的地址,應更改端口(第22行)和stmp.gmail.com (第37行)。
Here is what the email alert received should look like:
收到的電子郵件警報如下所示:
使用任務計劃程序 (Using the Task Scheduler)
All this is a good start but it would be a waste of time to constantly run the code manually until a good deal is found. Might as well look for deals on Kayak manually, like anybody would.
所有這一切都是一個好的開始,但是要不斷地手動運行代碼直到找到大量交易,這將浪費時間。 像任何人一樣,還可以手動在Kayak上尋找交易。
Thankfully, task schedulers exist. The idea is simple. You can schedule for your code to run N amount of times per day for however long you want. You can even have it work when the computer is sleeping (not turned off however).
幸運的是,任務計劃程序存在。 這個想法很簡單。 您可以計劃每天將代碼運行N次,無論時間長短。 您甚至可以在計算機處于Hibernate狀態時使它正常工作(但是未關閉)。
For a tutorial on how to setup the Windows Task Scheduler with a python script, go here. Using this tool can be super useful for many situations and I highly recommend you learn to use it!
有關如何使用python腳本設置Windows Task Scheduler的教程,請轉到此處 。 在許多情況下使用此工具都非常有用,我強烈建議您學習使用它!
You can now let your computer search for cheap flights while you sit back and have a beer. You just saved both time and money, all of it for free.
現在,當您坐下來喝啤酒時,可以讓計算機搜索廉價航班。 您只是節省了時間和金錢,所有這些都是免費的。
Thanks a lot for reading!
非常感謝您的閱讀!
Full code available here.
完整的代碼在這里 。
翻譯自: https://towardsdatascience.com/how-to-find-cheap-flights-in-80-lines-of-code-ba4f492587db
廉價raid
總結
以上是生活随笔為你收集整理的廉价raid_如何查找80行代码中的廉价航班的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洗衣机减速器的种类有哪些
- 下一篇: 删除数据库的sql语句是什么(数据库实验