forked from gaspa93/googlemaps-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglemaps.py
executable file
·334 lines (244 loc) · 11.1 KB
/
googlemaps.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
# -*- coding: utf-8 -*-
import pandas as pd
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
from datetime import datetime
import time
import re
import logging
import traceback
import numpy as np
import itertools
GM_WEBPAGE = 'https://www.google.com/maps/'
MAX_WAIT = 10
MAX_RETRY = 5
MAX_SCROLLS = 40
class GoogleMapsScraper:
def __init__(self, debug=False):
self.debug = debug
self.driver = self.__get_driver()
self.logger = self.__get_logger()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, tb):
if exc_type is not None:
traceback.print_exception(exc_type, exc_value, tb)
self.driver.close()
self.driver.quit()
return True
def sort_by(self, url, ind):
self.driver.get(url)
wait = WebDriverWait(self.driver, MAX_WAIT)
# open dropdown menu
clicked = False
tries = 0
while not clicked and tries < MAX_RETRY:
try:
#if not self.debug:
# menu_bt = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.cYrDcjyGO77__container')))
#else:
menu_bt = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@data-value=\'Sort\']')))
menu_bt.click()
clicked = True
time.sleep(3)
except Exception as e:
tries += 1
self.logger.warn('Failed to click sorting button')
# failed to open the dropdown
if tries == MAX_RETRY:
return -1
# element of the list specified according to ind
recent_rating_bt = self.driver.find_elements_by_xpath('//li[@role=\'menuitemradio\']')[ind]
recent_rating_bt.click()
# wait to load review (ajax call)
time.sleep(5)
return 0
def get_places(self, method='urls', keyword_list=None):
df_places = pd.DataFrame()
if method == 'urls':
# search_point_url = row['url'] # TODO:
pass
if method == 'squares':
search_point_url_list = self._gen_search_points_from_square(keyword_list=keyword_list)
else:
# search_point_url = f"https://www.google.com/maps/search/{row['keyword']}/@{str(row['longitude'])},{str(row['latitude'])},{str(row['zoom'])}z"
# TODO:
pass
for i, search_point_url in enumerate(search_point_url_list):
if (i+1) % 10 == 0:
print(f"{i}/{len(search_point_url_list)}")
df_places = df_places[['search_point_url', 'href', 'name', 'rating', 'num_reviews', 'close_time', 'other']]
df_places.to_csv('output/places_wax.csv', index=False)
try:
self.driver.get(search_point_url)
except NoSuchElementException:
self.driver.quit()
self.driver = self.__get_driver()
self.driver.get(search_point_url)
# Gambiarra to load all places into the page
scrollable_div = self.driver.find_element_by_css_selector(
"div.siAUzd-neVct.section-scrollbox.cYB2Ge-oHo7ed.cYB2Ge-ti6hGc > div[aria-label*='Results for']")
for i in range(10):
self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', scrollable_div)
# Get places names and href
# time.sleep(2)
response = BeautifulSoup(self.driver.page_source, 'html.parser')
div_places = response.select('div[jsaction] > a[href]')
# print(len(div_places))
for div_place in div_places:
place_info = {
'search_point_url': search_point_url.replace('https://www.google.com/maps/search/', ''),
'href': div_place['href'],
'name': div_place['aria-label'],
'rating': None,
'num_reviews': None,
'close_time': None,
'other': None
}
df_places = df_places.append(place_info, ignore_index=True)
df_places = df_places[['search_point_url', 'href', 'name', 'rating', 'num_reviews', 'close_time', 'other']]
df_places.to_csv('output/places_wax.csv', index=False)
self.driver.quit()
def _gen_search_points_from_square(self, keyword_list=None):
# TODO: Generate search points from corners of square
keyword_list = [] if keyword_list is None else keyword_list
square_points = pd.read_csv('input/square_points.csv')
cities = square_points['city'].unique()
search_urls = []
for city in cities:
df_aux = square_points[square_points['city'] == city]
latitudes = np.linspace(df_aux['latitude'].min(), df_aux['latitude'].max(), num=20)
longitudes = np.linspace(df_aux['longitude'].min(), df_aux['longitude'].max(), num=20)
coordinates_list = list(itertools.product(latitudes, longitudes, keyword_list))
search_urls += [f"https://www.google.com/maps/search/{coordinates[2]}/@{str(coordinates[1])},{str(coordinates[0])},{str(15)}z"
for coordinates in coordinates_list]
return search_urls
def get_reviews(self, offset):
# scroll to load reviews
# wait for other reviews to load (ajax)
time.sleep(4)
self.__scroll()
# expand review text
self.__expand_reviews()
# parse reviews
response = BeautifulSoup(self.driver.page_source, 'html.parser')
rblock = response.find_all('div', class_='ODSEW-ShBeI NIyLF-haAclf gm2-body-2')
parsed_reviews = []
for index, review in enumerate(rblock):
if index >= offset:
parsed_reviews.append(self.__parse(review))
# logging to std out
print(self.__parse(review))
return parsed_reviews
def get_account(self, url):
self.driver.get(url)
# ajax call also for this section
time.sleep(4)
resp = BeautifulSoup(self.driver.page_source, 'html.parser')
place_data = self.__parse_place(resp)
return place_data
def __parse(self, review):
item = {}
id_review = review.find('button', class_='ODSEW-ShBeI-JIbuQc-menu ODSEW-ShBeI-JIbuQc-menu-SfQLQb-title')['data-review-id']
username = review.find('div', class_='ODSEW-ShBeI-title').find('span').text
try:
review_text = self.__filter_string(review.find('span', class_='ODSEW-ShBeI-text').text)
except Exception as e:
review_text = None
rating = float(review.find('span', class_='ODSEW-ShBeI-H1e3jb')['aria-label'].split(' ')[1])
relative_date = review.find('span', class_='ODSEW-ShBeI-RgZmSc-date').text
try:
n_reviews_photos = review.find('div', class_='section-review-subtitle').find_all('span')[1].text
metadata = n_reviews_photos.split('\xe3\x83\xbb')
if len(metadata) == 3:
n_photos = int(metadata[2].split(' ')[0].replace('.', ''))
else:
n_photos = 0
idx = len(metadata)
n_reviews = int(metadata[idx - 1].split(' ')[0].replace('.', ''))
except Exception as e:
n_reviews = 0
n_photos = 0
user_url = review.find('a')['href']
item['id_review'] = id_review
item['caption'] = review_text
# depends on language, which depends on geolocation defined by Google Maps
# custom mapping to transform into date shuold be implemented
item['relative_date'] = relative_date
# store datetime of scraping and apply further processing to calculate
# correct date as retrieval_date - time(relative_date)
item['retrieval_date'] = datetime.now()
item['rating'] = rating
item['username'] = username
item['n_review_user'] = n_reviews
item['n_photo_user'] = n_photos
item['url_user'] = user_url
return item
def __parse_place(self, response):
place = {}
try:
place['overall_rating'] = float(response.find('div', class_='gm2-display-2').text.replace(',', '.'))
except:
place['overall_rating'] = 'NOT FOUND'
try:
place['n_reviews'] = int(response.find('div', class_='gm2-caption').text.replace('.', '').replace(',','').split(' ')[0])
except:
place['n_reviews'] = 0
return place
# expand review description
def __expand_reviews(self):
# use XPath to load complete reviews
links = self.driver.find_elements_by_xpath('//button[@class=\'section-expand-review blue-link\']')
for l in links:
l.click()
time.sleep(2)
def __scroll(self):
scrollable_div = self.driver.find_element_by_css_selector('div.siAUzd-neVct.section-scrollbox.cYB2Ge-oHo7ed.cYB2Ge-ti6hGc')
self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', scrollable_div)
#self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
def __get_logger(self):
# create logger
logger = logging.getLogger('googlemaps-scraper')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
fh = logging.FileHandler('gm-scraper.log')
fh.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# add formatter to ch
fh.setFormatter(formatter)
# add ch to logger
logger.addHandler(fh)
return logger
def __get_driver(self, debug=False):
options = Options()
if not self.debug:
options.add_argument("--headless")
else:
options.add_argument("--window-size=1366,768")
options.add_argument("--disable-notifications")
options.add_argument("--lang=en-GB")
input_driver = webdriver.Chrome(executable_path=ChromeDriverManager(log_level=0).install(), options=options)
# first lets click on google agree button so we can continue
try:
input_driver.get(GM_WEBPAGE)
agree = WebDriverWait(input_driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//span[contains(text(), "I agree")]')))
agree.click()
# back to the main page
input_driver.switch_to_default_content()
except:
pass
return input_driver
# util function to clean special characters
def __filter_string(self, str):
strOut = str.replace('\r', ' ').replace('\n', ' ').replace('\t', ' ')
return strOut