-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHotel_Traveloka_WithDef.py
348 lines (287 loc) · 14.4 KB
/
Hotel_Traveloka_WithDef.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
@author : hhtrieu0108
github : https://github.com/hhtrieu0108
linkedin : https://www.linkedin.com/in/trieuhh
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import datetime
import pandas as pd
import numpy as np
def process_rating(x):
if x == 'No value':
return x
elif x == 'Chưa có đánh giá nào':
return "No value"
else:
return x.split(' ')[3].replace('.', '')
def process_price(x):
if x == 'No value':
return x
else:
return x.split(' ')[0].replace('.', '')
def process_score(x):
if x == '-':
return "No value"
else:
return x
def get_url(list_of_places):
"""
:param list_of_places: list of place you want to crawl. But need to take from url of traveloka
:return: list of url from traveloka
"""
list_of_url = []
time = datetime.datetime.today().date()
next_time = time + datetime.timedelta(days=1)
for place in list_of_places:
url = f"https://www.traveloka.com/vi-vn/hotel/search?spec={time.strftime(format='%d-%m-%Y')}.{next_time.strftime(format='%d-%m-%Y')}.1.1.HOTEL_GEO.{place}.1"
list_of_url.append(url)
return list_of_url
def crawl_data(list_of_url,list_of_places):
"""
:param list_of_url: Url of the traveloka web
:return: dataframe in csv file and dictionary of dataframe with key is place and value is dataframe
"""
driver = webdriver.Edge()
hotel_by_place = {}
for url,place in zip(list_of_url,list_of_places):
driver.get(url)
sleep(10)
current_window = driver.current_window_handle
def get_hotels():
return driver.find_elements(By.XPATH, "//div[@class='css-1dbjc4n'][@data-testid='tvat-searchListItem']")
df_traveloka = pd.DataFrame(columns=['hotel_names', 'location', 'price', 'score_hotels',
'number_rating', 'star_number', 'received_time',
'giveback_time', 'description', 'hotel_link'])
list_hotel_exist = []
hotel_names = []
location_texts = []
price_text = []
description_text = []
star_number_texts = []
hotel_link = []
score_hotels = []
number_rating_text = []
received_time = []
giveback_time = []
num_steps = 5000
while True:
previous_scroll_position = driver.execute_script("return window.scrollY")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
sleep(2)
new_scroll_position = driver.execute_script("return window.scrollY")
if new_scroll_position <= previous_scroll_position:
break
driver.execute_script("window.scrollTo(0, 0)")
for step in range(num_steps):
current_position = driver.execute_script("return window.scrollY")
if current_position == new_scroll_position:
break
try:
list_hotel = get_hotels()
for index in range(len(list_hotel)):
if list_hotel[index] in list_hotel_exist:
continue
ActionChains(driver).move_to_element(list_hotel[index]).click().perform()
list_hotel_exist.append(list_hotel[index])
driver.switch_to.window(driver.window_handles[1])
wait_time = 10
try:
hotel_name_elements = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//h1[@class='css-4rbku5 css-901oao css-cens5h r-cwxd7f r-t1w4ow r-1ui5ee8 r-b88u0q r-nwxazl r-fdjqy7']"))
)
except:
print("Error hotel")
print("Link: ", driver.current_url)
print(hotel_name_elements)
try:
hotel_name_elements = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//h1[@class='css-4rbku5 css-901oao css-cens5h r-t1w4ow r-1ui5ee8 r-b88u0q r-nwxazl r-fdjqy7']"))
)
except:
print("Error hotel 2")
print("Link: ", driver.current_url)
try:
location_elements = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao css-cens5h r-cwxd7f r-13awgt0 r-t1w4ow r-1b43r93 r-majxgm r-rjixqe r-fdjqy7']"))
)
except:
print("Error location")
print("Link: ", driver.current_url)
try:
location_elements = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao css-cens5h r-13awgt0 r-t1w4ow r-1b43r93 r-majxgm r-rjixqe r-fdjqy7']"))
)
except:
print("Error location 2")
print("Link: ", driver.current_url)
try:
price_elements = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao r-t1w4ow r-1x35g6 r-b88u0q r-vrz42v r-fdjqy7']"))
)
except:
print("Error in price")
print("Link: ", driver.current_url)
try:
star_number = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-1dbjc4n']//div[@class='css-1dbjc4n r-18u37iz']//div[@class='css-1dbjc4n r-18u37iz']"))
)
except:
print("Error in star number")
print("Link: ", driver.current_url)
try:
score = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao r-jwli3a r-t1w4ow r-adyw6z r-b88u0q r-135wba7 r-fdjqy7']"))
)
except:
print("Error in score")
print("Link: ", driver.current_url)
try:
number_rating = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao r-jwli3a r-t1w4ow r-1enofrn r-b88u0q r-1cwl3u0 r-fdjqy7']"))
)
except:
print("Error in rating")
print("Link: ", driver.current_url)
try:
time = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-901oao r-1h9nbw7 r-t1w4ow r-1b43r93 r-b88u0q r-rjixqe r-fdjqy7']"))
)
except:
print("Error in time")
print("Link: ", driver.current_url)
try:
for hotel_name_element in hotel_name_elements:
hotel_names.append(hotel_name_element.text)
except:
print("Error occurred while retrieving hotel names")
try:
for location_element in location_elements:
location_texts.append(location_element.text.split('\n')[0])
except:
print("Error location")
try:
for original_price_element in price_elements:
price_text.append(original_price_element.text)
except:
print("Error in price")
try:
for star_number_element in star_number:
star_number_texts.append(len(star_number_element.find_elements(By.TAG_NAME, "img")))
except:
print("Error star")
try:
for rating in number_rating:
number_rating_text.append(rating.text)
except:
print("Error in rating")
try:
for score_hotel in score:
score_hotels.append(score_hotel.text)
except:
print("Error in score")
try:
for received in time[::2]:
received_time.append(received.text)
except:
print("Error in received")
try:
for giveback in time[1::2]:
giveback_time.append(giveback.text)
except:
print("Error in giveback")
try:
hotel_link.append(driver.current_url)
except:
print("Error in link")
driver.execute_script(f"window.scrollTo(0, 20);")
try:
description_hotel = WebDriverWait(driver, wait_time).until(
EC.visibility_of_element_located((
By.XPATH,
"//div[@class='css-18t94o4 css-1dbjc4n r-kdyh1x r-1loqt21 r-10paoce r-5njf8e r-1otgn73 r-lrvibr']"))
)
except:
print("Error in button")
description_hotel.click()
detail_description = WebDriverWait(driver, wait_time).until(
EC.visibility_of_all_elements_located((
By.XPATH,
"//div[@class='css-1dbjc4n r-13awgt0 r-1rnoaur']//div[@class='css-1dbjc4n r-f4gmv6 r-nsbfu8']"))
)
for description in detail_description:
description_text.append(description.text)
driver.close()
driver.switch_to.window(current_window)
for check in [hotel_names, location_texts, price_text,
score_hotels, number_rating_text,star_number_texts,
received_time, giveback_time, description_text, hotel_link]:
if check == []:
check.append("No value")
df_traveloka_new = pd.DataFrame(list(zip(hotel_names, location_texts, price_text,
score_hotels, number_rating_text, star_number_texts,
received_time, giveback_time, description_text, hotel_link)),
columns=['hotel_names', 'location', 'price', 'score_hotels',
'number_rating', 'star_number', 'received_time',
'giveback_time', 'description', 'hotel_link'])
df_traveloka = pd.concat((df_traveloka, df_traveloka_new), axis=0, ignore_index=True)
hotel_names = []
location_texts = []
price_text = []
description_text = []
star_number_texts = []
hotel_link = []
score_hotels = []
number_rating_text = []
received_time = []
giveback_time = []
except Exception as e:
print("Error:", e)
print(driver.current_url)
df_traveloka_nodup = df_traveloka.drop_duplicates('hotel_names').reset_index(drop=True)
hotel_by_place[place] = df_traveloka_nodup
hotel_by_place[place].to_csv(f"Hotel_{place}_Traveloka.csv",index=False)
driver.quit()
return hotel_by_place
def processing_data(list_of_dataframe):
"""
:param list_of_dataframe: list of dataframe with key is place and value is dataframe
:return: processed dataframe in csv file
"""
processed_hotel = {}
for place,data in zip(list_of_dataframe.keys(),list_of_dataframe.values()):
data['number_rating'] = data['number_rating'].apply(process_rating)
data['price'] = data['price'].apply(process_price)
data['score_hotels'] = data['score_hotels'].apply(process_score)
data['id'] = np.arange(1, len(data) + 1)
data.to_csv(f"Processed_Data/Hotel_{place}_Processed.csv",index=False)
processed_hotel[place] = data
return processed_hotel
if __name__ == "__main__":
list_of_places = ['10010498.Nha%20Trang','10009888.Thành%20phố%20Vũng%20Tàu',
'10010083.Đà%20Nẵng','10009843.Hà%20Nội','10009794.Thành%20phố%20Hồ%20Chí%20Minh']
list_of_url = get_url(list_of_places)
hotel_by_place = crawl_data(list_of_places=list_of_places,list_of_url=list_of_url)
processing_hotel = processing_data(list_of_dataframe=hotel_by_place)