-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_google_maps_scraper.py
268 lines (213 loc) · 8.32 KB
/
Main_google_maps_scraper.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
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.keys import Keys
from selenium.webdriver import ActionChains
import csv
import threading
import time
try:
import tkinter as tk
from tkinter import messagebox as msg, filedialog
from tkinter import *
except:
import Tkinter as tk
import tkMessageBox as msg
from Tkinter import *
from GUI import *
def open_url(url, num_retries=5):
try:
driver = webdriver.Chrome()
driver.get(url)
except:
if num_retries > 0:
driver.quit()
open_url(url, num_retries-1)
return driver
class google_maps_scraper():
def __init__(self, master):
# self.keyword = keyword
self.start_url = 'https://www.google.com/maps/search/'
self.master = master
self.total_data = []
self.gui = main_GUI(master, self.total_data, self.endApplication)
self.running = 1
self.periodicCall()
self.i = 0
self.url_dict = []
self.company_names = []
#self.go_to_search()
def periodicCall(self):
if self.running == 0:
print('Exit')
import sys
#self.master.quit()
self.master.destroy()
sys.exit(1)
elif self.running == 1:
if self.gui.start == True:
self.keyword = self.gui.keyword
self.running = 2
self.thread1 = threading.Thread(target=self.go_to_search)
self.thread1.start()
elif self.running == 2:
self.gui.insert_data()
self.master.after(2000, self.periodicCall)
def endApplication(self):
self.running = 0
def go_to_search(self):
filename = self.keyword + '.csv'
self.output_file = open(filename, 'w', encoding='utf-8', newline='')
self.writer = csv.writer(self.output_file)
headers = ['Company Name', 'Address', 'Category', 'Star Rating', 'Number of Reviews',
'Phone Number', 'Website']
self.writer.writerow(headers)
self.driver = open_url(self.start_url)
input = WebDriverWait(self.driver, 50).until(
EC.presence_of_element_located((By.XPATH, "//input[@aria-label='Search Google Maps']")))
search_btn = WebDriverWait(self.driver, 50).until(
EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Search']")))
input.send_keys(self.keyword)
time.sleep(2)
search_btn.click()
self.scraping_onepage()
self.running = 0
def scraping_onepage(self):
self.item_index = 0
self.skip_index = 0
while 1:
if len(self.company_names) >= 50 or self.skip_index >= 80:
break
print(self.item_index)
companies = WebDriverWait(self.driver, 50).until(
EC.presence_of_all_elements_located((By.XPATH, "//h3[@class='section-result-title']")))
self.item_cnt = len(companies)
if companies[self.item_index].text not in self.company_names:
self.skip_index = 0
self.company_names.append(companies[self.item_index].text)
self.org_url = self.driver.current_url
companies[self.item_index].click()
print(self.org_url)
self.scraping_onecompany()
self.driver.get(self.org_url)
time.sleep(5)
else:
self.skip_index += 1
self.item_index += 1
if self.item_index == self.item_cnt:
self.item_index = 0
#self.driver.delete_all_cookies()
next_btn = WebDriverWait(self.driver, 50).until(
EC.element_to_be_clickable((By.XPATH, "//button[@id='section-pagination-button-next']")))
next_btn.click()
print('Next Button: Clicked Successfully')
time.sleep(5)
companies = WebDriverWait(self.driver, 50).until(
EC.presence_of_all_elements_located((By.XPATH, "//h3[@class='section-result-title']")))
self.item_cnt = len(companies)
if self.item_cnt == 0:
break
self.driver.quit()
self.output_file.close()
def scraping_onecompany(self):
i = 0
while 1:
if i > 200:
company_name = ''
break
else:
i += 1
company_name = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, "//h1[@class='section-hero-header-title']"))
)
if company_name.text is not '':
company_name = company_name.text
break
try:
rating = self.driver.find_element_by_css_selector('span.section-star-display').text
except:
rating = ''
try:
reviews = self.driver.find_element_by_css_selector('ul.section-rating-term-list').find_element_by_css_selector(
'button.widget-pane-link').text
except:
reviews = ''
section_infos = self.driver.find_elements_by_css_selector('div.section-info')
try:
address = self.driver.find_elements_by_css_selector('div.section-info')[0].text
except:
address = ''
website = ''
phone = ''
for section_info in section_infos:
try:
#website_lbl = section_info.find_element_by_xpath("//span[@aria-label='Website']")
website_lbl = section_info.find_element_by_css_selector("span.section-info-icon.maps-sprite-pane-info-website")
website = website_lbl.find_element_by_xpath('..').find_element_by_css_selector('span.section-info-text').text
except:
pass
try:
#phone_lbl = section_info.find_element_by_xpath("//span[@aria-label='Phone']")
phone_lbl = section_info.find_element_by_css_selector("span.section-info-icon.maps-sprite-pane-info-phone")
phone = phone_lbl.find_element_by_xpath('..').find_element_by_css_selector('span.section-info-text').text
except:
pass
try:
category = self.driver.find_element_by_css_selector('span.section-rating-term').text
except:
category = ''
element = {
'company_name': company_name,
'address': address,
'category': category,
'star_rating': rating,
'review_numbers': reviews,
'phone': phone,
'websites': website
}
#if element not in self.total_data:
print(element)
self.total_data.append(element)
# driver.execute_script("window.history.go(-1)")
row = [
element['company_name'],
element['address'],
element['category'],
element['star_rating'],
element['review_numbers'],
element['phone'],
element['websites'],
]
self.writer.writerow(row)
def save_to_csv(self):
filename = self.keyword + '.csv'
self.output_file = open(filename, 'w', encoding='utf-8', newline='')
self.writer = csv.writer(self.output_file)
headers = ['Company Name', 'Address', 'Category', 'Star Rating', 'Number of Reviews',
'Phone Number', 'Website']
self.writer.writerow(headers)
for i, element in enumerate(self.total_data):
row = [
element['company_name'],
element['address'],
element['category'],
element['star_rating'],
element['review_numbers'],
element['phone'],
element['websites'],
]
self.writer.writerow(row)
self.output_file.close()
if __name__ == '__main__':
start_time = time.time()
keywords = [
'solar sacramento ca',
'Solar San Diego Ca'
]
root = tk.Tk()
app = google_maps_scraper(root)
root.mainloop()
elapsed_time = time.time() - start_time
print(elapsed_time)