-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctripplus.py
304 lines (235 loc) · 10.5 KB
/
ctripplus.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
#!/usr/bin/env python
# coding=utf-8
import pprint
import csv
import click
import requests
import datetime as datetime
# from splinter import Browser
import time
import re
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.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException
def validate_d(date_text):
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
def find_entry(entry_dict, room_name):
for entry in entry_dict:
pprint.pprint(entry)
if entry['GTA_Room_Name'] == room_name:
return entry
return None
@click.command()
@click.option('--filename', default='ctrip_hotel_ids')
@click.option('--days', default=30, type=int)
def ctripplus(filename, days):
pp = pprint
res = []
# from_date = datetime.datetime.today().date() + datetime.timedelta(days=30)
from_date = datetime.datetime.today().date() + datetime.timedelta(days=days)
to_date = from_date + datetime.timedelta(days=1)
# validate_d(from_d)
# validate_d(to_d)
# from_date = datetime.datetime.strptime(from_d, '%Y-%m-%d').date()
# to_date = datetime.datetime.strptime(to_d, '%Y-%m-%d').date()
# with Browser() as b:
driver = webdriver.Firefox()
driver.implicitly_wait(10)
# driver = webdriver.Ie()
hotel_ids = set()
with open(filename, encoding='utf-8-sig') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# pp.pprint(row['hotel_id'])
hotel_ids.add(row['hotel_id'])
for counter, hotel_id in enumerate(hotel_ids):
pp.pprint('Counter: ' + str(counter))
from_date_plus_one = from_date + datetime.timedelta(days=1)
url = 'http://hotels.ctrip.com/international/' + \
str(hotel_id) + '.html?' + \
'CheckIn=' + from_date.strftime('%Y-%m-%d') + \
'&CheckOut=' + from_date_plus_one.strftime('%Y-%m-%d') + \
'&fixsubhotel=t'
pp.pprint(url)
try:
driver.get(url)
except TimeoutException:
continue
# element = WebDriverWait(driver, 15).until(
# lambda driver: driver.execute_script("return $.isLoaded == true")
# )
time.sleep(5)
entry = {}
entry_dict = {}
try:
hotel_name = driver.find_element_by_css_selector('h1.name').text
except NoSuchElementException:
continue
except WebDriverException:
continue
if len(driver.find_elements_by_css_selector('div.hroom_tr.J_baseRoomlist')) == 0:
# logging hotels with no price...
ent = {}
ent['ctrip_hotel_id'] = hotel_id
ent['Hotel_Name'] = hotel_name
ent['Check_in'] = from_date.strftime('%Y-%m-%d')
ent['GTA_Price(on ctrip)'] = -1
res.append(ent)
continue
# entry_dict = {}
for i, elem in enumerate(driver.find_elements_by_css_selector('div.hroom_tr.J_baseRoomlist')):
# pp.pprint(elem)
room_name = elem.find_element_by_css_selector('dt.hroom_base_tit').text
pp.pprint(room_name)
entry_dict[str(i)] = {}
entry_dict[str(i)]['ctrip_hotel_id'] = hotel_id
entry_dict[str(i)]['Hotel_Name'] = hotel_name
entry_dict[str(i)]['Check_in'] = from_date.strftime('%Y-%m-%d')
entry_dict[str(i)]['count'] = 0
for rp in elem.find_elements_by_css_selector('div.hroom_tr_col.J_subRoomlist'):
# pp.pprint(rp)
try:
book_btn = rp.find_element_by_css_selector('button.J_bookNowBtn.btns_base22')
except NoSuchElementException:
pp.pprint('Warning: No booking button...')
continue
try:
# entry = dict()
# entry['ctrip_hotel_id'] = hotel_id
# entry['Hotel_Name'] = hotel_name
# entry['GTA_Room_Name'] = room_name
entry_dict[str(i)]['GTA_Room_Name'] = room_name
entry_dict[str(i)]['GTA_Room_Description'] = rp.find_element_by_css_selector('.hroom_roomname.J_rooms_name').text or ''
# entry['RP_id'] = book_btn.get_attribute('id').rstrip().split('_')[2]
entry_dict[str(i)]['GTA_Bed'] = rp.find_element_by_css_selector('.hroom_col.hroom_col_bed.J_facilitiesInfoLogo').text or ''
n_guest_ele = rp.find_element_by_css_selector('.hroom_col.hroom_col_people')
entry_dict[str(i)]['GTA_guest_#'] = re.sub('[^0-9]', '', n_guest_ele.find_element_by_tag_name('span').get_attribute('title'))
entry_dict[str(i)]['GTA_Breakfast'] = rp.find_element_by_css_selector('.abbr.J_facilitiesInfoLogo').text
entry_dict[str(i)]['GTA_Policy'] = ''.join(rp.find_element_by_css_selector('.abbr.J_cancelBookingLogo').text.split())
# entry['GTA_Check_in'] = from_date.strftime('%Y-%m-%d')
entry_dict[str(i)]['GTA_Price(on ctrip)'] = re.sub('[^0-9]', '', rp.find_element_by_css_selector('.txt_taxtips').text)
# res.append(entry)
except StaleElementReferenceException:
pp.pprint("Error: Elem became stale... fffffffff...")
except NoSuchElementException:
pp.pprint("Error: No such elem... fff...")
break
# second run..
url = 'http://hotels.ctrip.com/international/' + \
str(hotel_id) + '.html?' + \
'CheckIn=' + from_date.strftime('%Y-%m-%d') + \
'&CheckOut=' + from_date_plus_one.strftime('%Y-%m-%d')
pp.pprint(url)
try:
driver.get(url)
except TimeoutException:
continue
time.sleep(7)
# element = WebDriverWait(driver, 15).until(
# lambda driver: driver.execute_script("return $.isLoaded == true")
# )
# time.sleep(10)
try:
driver.find_elements_by_css_selector('a.foldMe')[-1].click()
except ElementNotVisibleException:
pp.pprint('Warning: No expanding...')
except IndexError:
pp.pprint('Error: a.foldMe out of bound...')
except WebDriverException:
pp.pprint('Error: WebDriverException when clicking expanding all...')
time.sleep(3)
# pprint.pprint(entry_dict)
for i, elem in enumerate(driver.find_elements_by_css_selector('div.hroom_tr.J_baseRoomlist')):
# pp.pprint(elem)
room_name = elem.find_element_by_css_selector('dt.hroom_base_tit').text
# pp.pprint(room_name)
entry = None
# entry = find_entry(entry_dict, room_name)
# for key, value in entry_dict.items():
# # pprint.pprint(entry)
# if entry_dict[key]['GTA_Room_Name'] == room_name:
# entry = entry_dict[key]
for rp in elem.find_elements_by_css_selector('div.hroom_tr_col.J_subRoomlist'):
# pp.pprint(rp)
try:
book_btn = rp.find_element_by_css_selector('button.J_bookNowBtn.btns_base22')
except NoSuchElementException:
pp.pprint('Warning: No booking button...')
continue
try:
n_guest_ele = rp.find_element_by_css_selector('.hroom_col.hroom_col_people')
n_guest = re.sub('[^0-9]', '', n_guest_ele.find_element_by_tag_name('span').get_attribute('title'))
# n_guests = re.sub('[^0-9]', '', rp.find_element_by_css_selector('.txt_taxtips').get('title'))
for key, value in entry_dict.items():
# pprint.pprint(entry)
if entry_dict[key]['GTA_Room_Name'] == room_name and entry_dict[key]['GTA_guest_#'] == n_guest:
entry = entry_dict[key]
# entry = dict()
if entry == None:
continue
breakfast = rp.find_element_by_css_selector('.abbr.J_facilitiesInfoLogo').text
policy = ''.join(rp.find_element_by_css_selector('.abbr.J_cancelBookingLogo').text.split())
if entry['GTA_Breakfast'] != breakfast:
continue
# if not( (('不可取消' in policy) and ('不可取消' in entry['GTA_Policy'])) or ( ('免费取消' in policy) and ('免费取消' in entry['GTA_Policy']) ) ):
# continue
if (('不可取消' in policy) and ('免费取消' in entry['GTA_Policy'])) or ( ('免费取消' in policy) and ('不可取消' in entry['GTA_Policy']) ) :
continue
entry['Ctrip_room_name_' + str(entry['count'])] = room_name
entry['Ctrip_room_description_' + str(entry['count'])] = rp.find_element_by_css_selector('.hroom_roomname.J_rooms_name').text or ''
# entry['RP_id'] = book_btn.get_attribute('id').rstrip().split('_')[2]
entry['Ctrip_Bed_' + str(entry['count'])] = rp.find_element_by_css_selector('.hroom_col.hroom_col_bed.J_facilitiesInfoLogo').text or ''
entry['Ctrip_guest_#_' + str(entry['count'])] = n_guest
entry['ctrip_Breakfast_' + str(entry['count'])] = breakfast
# entry['ctrip_Breakfast_' + str(entry['count'])] = rp.find_element_by_css_selector('.abbr.J_facilitiesInfoLogo').text
entry['ctrip_Policy_' + str(entry['count'])] = policy
# entry['ctrip_Policy_' + str(entry['count'])] = ''.join(rp.find_element_by_css_selector('.abbr.J_cancelBookingLogo').text.split())
entry['ctrip_lowest_Price_' + str(entry['count'])] = re.sub('[^0-9]', '', rp.find_element_by_css_selector('.txt_taxtips').text)
gta_price = float(entry['GTA_Price(on ctrip)'])
ctrip_price = float(entry['ctrip_lowest_Price_' + str(entry['count'])])
entry['diff_percentage_' + str(entry['count'])] = float('{0:.3f}'.format(float( (ctrip_price - gta_price) / gta_price)))
entry['count'] += 1
# res.append(entry)
except StaleElementReferenceException:
pp.pprint("Error: Elem became stale... fffffffff...")
except ValueError:
pp.pprint("Error: Value error when converting to float... fffffffff...")
except NoSuchElementException:
pp.pprint("Error: No such elem... ffff... ")
except KeyError:
pp.pprint("Error: key error for entry[] ...")
break
for key, value in entry_dict.items():
res.append(entry_dict[key])
# res.append(entry)
driver.quit()
# pp.pprint(res)
if not res:
pprint.pprint("List is empty! No price found for every hotel in the file!")
return
keys_max = None
k_max = 0
for ent in res:
if len(ent.keys()) > k_max:
keys_max = ent.keys()
k_max = len(ent.keys())
# keys = res[0].keys()
with open('output_Ctripplus_' + datetime.datetime.now().strftime('%y%m%d_%H%M') + '_' + str(days) + 'days.csv', 'w', newline='', encoding='utf-8') as output_file:
# dict_writer = csv.DictWriter(output_file, keys)
dict_writer = csv.DictWriter(output_file, keys_max)
dict_writer.writeheader()
dict_writer.writerows(res)
if __name__ == '__main__':
ctripplus()