-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooking_scrape_eric_shaney.py
182 lines (143 loc) · 5.05 KB
/
booking_scrape_eric_shaney.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#%%
#!pip3 install -r requirements.txt
#import dropbox
from selenium.webdriver.common.keys import Keys
# auxiliary functions modified by Luis.
import scrape_functions as kzd
import sys
import calendar
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, StaleElementReferenceException
import re
from selenium.webdriver.support.ui import Select
import random
import time
from selenium import webdriver
import os
import numpy as np
import importlib
importlib.reload(sys.modules['scrape_functions'])
from datetime import date
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.chrome.options import Options
import pandas as pd
# %% Starting code
dfolder = 'out'
link = 'https://www.booking.com/'
profile, browser, download_path = kzd.start_up(dfolder, link, download=True)
#%% Main Page
place = 'Barcelona'
search1 = browser.find_element('xpath','//input[@placeholder="Where are you going?"]')
search1.send_keys(place)
# kzd.check_and_click(
# browser, '.sb-searchbox__button', type='css')
kzd.check_and_click(browser,'//button[@type="submit"]', type='xpath')
print('main search button clicked')
#%% Search Page
time.sleep(2)
dates = browser.find_elements('xpath',
'//table[@class="aadb8ed6d3"]/tbody/tr/td/span')
print('dates: ', len(dates))
#%%
time.sleep(2)
check_in_date = "2023-01-28"
check_out_date = "2023-01-31"
#%%
def scrollDown(driver, numberOfScrollDowns):
body = driver.find_element(By.TAG_NAME, "body")
while numberOfScrollDowns >= 0:
body.send_keys(Keys.PAGE_DOWN)
numberOfScrollDowns -= 1
return driver
#%%
scrollDown(browser, 0.05)
e = browser.find_element(By.XPATH, '//span[@data-date="{}"]'.format(check_in_date))
browser.execute_script("arguments[0].click()", e)
e = browser.find_element(By.XPATH, '//span[@data-date="{}"]'.format(check_out_date))
browser.execute_script("arguments[0].click()", e)
time.sleep(1)
# click on empty space
kzd.check_and_click(
browser, '//input[@class="ce45093752"]', type='xpath')
print('blank space clicked')
time.sleep(1)
kzd.check_and_click(browser,'//button[@type="submit"]', type='xpath')
print('search button clicked')
time.sleep(1)
#%% LOOPING OVER WEBPAGES:
"""
This function is to obtain total number of pages.
To loop, just insert the full code on a big loop and use the pages output.
ADDITION: You can also add random waiting times to avoid getting banned from webpages.
"""
def get_number_pages(browser):
'''
Get the number of pages.
'''
a = browser.find_elements('xpath',
'//button[text() and @class="fc63351294 f9c5690c58"]')
total_pages = int(a[-1].text)
time.sleep(1)
return(total_pages)
pages = get_number_pages(browser)
print('total pages: ', pages)
#%%
def BookingReport(deal_boxes):
page_report = []
for deal_box in deal_boxes:
hotel_name = deal_box.find_element(By.CSS_SELECTOR,
'div[data-testid="title"]'
).get_attribute('innerHTML').strip()
if not deal_box.find_elements(By.CSS_SELECTOR, 'span[data-testid="price-and-discounted-price"]'):
hotel_price = deal_box.find_element(By.CSS_SELECTOR,
'div[data-testid="price-and-discounted-price"]'
).find_element(By.TAG_NAME, 'div').get_attribute('innerHTML').strip()
else:
hotel_price = deal_box.find_element(By.CSS_SELECTOR,
'span[data-testid="price-and-discounted-price"]'
).get_attribute('innerHTML').strip()
hotel_price = hotel_price.replace(" ", "")
hotel_price = hotel_price.replace(",", "")
hotel_price = int(hotel_price.replace("€", ""))
try:
hotel_score = deal_box.find_element(By.CSS_SELECTOR,
'div[aria-label*="Scored"]'
).get_attribute('innerHTML').strip()
except:
hotel_score = 'nan'
page_report.append(
[hotel_name, hotel_price, hotel_score]
)
# print([hotel_name, hotel_price, hotel_score])
return page_report
#%%
# Loop pages
full_report = []
for page in range(pages):
# Get the list of hotel elements on the page
print('Scraping page {} out of {}...'.format(page+1, pages))
print('before: ', len(full_report))
browser.implicitly_wait(2)
deal_boxes = browser.find_elements(
By.XPATH,
"//div[@data-testid='property-card']"
)
report = BookingReport(deal_boxes)
time.sleep(4)
full_report.extend(report)
print('after: ', len(full_report))
scrollDown(browser, 1.5)
kzd.check_and_click(browser,
'//button[@aria-label="Next page"]',
type='xpath' )
time.sleep(4)
report_df = pd.DataFrame(full_report)
# %%
deal_boxes = browser.find_elements(
By.XPATH,
"//div[@data-testid='property-card']"
)
report = BookingReport(deal_boxes)
# %%