Created
May 18, 2021 03:02
-
-
Save gourneau/0e3600a92e36bc2e308788f09f0ed42f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from pprint import pprint | |
import time | |
from statistics import mean, median, median_grouped | |
def get_prices(verbose=False): | |
cookies = {} | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0', | |
'Accept': '*/*', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'X-Requested-With': 'XMLHttpRequest', | |
'Origin': 'https://www.kayak.com', | |
'DNT': '1', | |
'Connection': 'keep-alive', | |
'Pragma': 'no-cache', | |
'Cache-Control': 'no-cache', | |
'TE': 'Trailers', | |
} | |
params = ( | |
('p', '0'), | |
) | |
start_date = "2021-05-24" | |
end_date = "2021-05-31" | |
url = f"/cars/Honolulu,HI-c28070/{start_date}/{end_date}" | |
searchId = "" | |
data = { | |
'searchId': searchId, | |
'url': url, | |
'contextualSearch': 'false', | |
'requestReason': 'POLL', | |
'pollNumber': '0', | |
'applyFilters': 'flase', | |
'driverAge': '-1', | |
'sortMode': 'rank', | |
'ascending': 'true', | |
'priceType': 'totaltaxes', | |
'showMap': 'flase', | |
'pageNumber': '1', | |
'textAdPageLocations': '', | |
'displayAdPageLocations': 'bottom-left,bottom' | |
} | |
response = requests.post('https://www.kayak.com/s/horizon/cars/results/CarSearchPollAction', headers=headers, params=params, cookies=cookies, data=data) | |
#NB. Original query string below. It seems impossible to parse and | |
#reproduce query strings 100% accurately so the one below is given | |
#in case the reproduced version is not "correct". | |
# response = requests.post('https://www.kayak.com/s/horizon/cars/results/CarSearchPollAction?p=0', headers=headers, cookies=cookies, data=data) | |
results = response.json()["CarResultsList"]["results"] | |
prices = [] | |
if verbose: | |
print(f'From {start_date} to {end_date}') | |
print("") | |
for k,v in results.items(): | |
try: | |
providers = v["providers"] | |
for provider in providers: | |
code = provider["code"] | |
price = provider["roundPrice"] | |
prices.append(price) | |
if verbose: | |
print(f'{code} ${price}') | |
except: | |
#print("failed") | |
pass | |
if verbose: | |
print("") | |
print(f"Avg price for time range {mean(prices)}") | |
print(f"median price for time range {median(prices)}") | |
return prices | |
if __name__ == '__main__': | |
target_sample_count = 150 | |
delay = 15 | |
total_prices = [] | |
while len(total_prices) <= target_sample_count: | |
total_prices.extend(get_prices(verbose=False)) | |
if delay: | |
print(f"Sleeping for {delay}") | |
time.sleep(delay) | |
print(f"Total price data points {len(total_prices)}") | |
print(f"Median {median(total_prices)}") | |
print(f"Median group {median_grouped(total_prices)}") | |
print(f"Lowest {min(total_prices)}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment