forked from omkarcloud/google-maps-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (97 loc) · 3.68 KB
/
main.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
from src.gmaps import Gmaps
from botasaurus import bt
import os
import shutil
love_it_star_it = '''Love It? Star It! ⭐ https://github.com/omkarcloud/google-maps-scraper/'''
try:
# Try to get the path using shutil
chrome_path = shutil.which("chrome") or shutil.which("google-chrome")
# Set the CHROME_PATH environment variable only if chrome_path is not None
if chrome_path is not None:
# Set the path to your Chrome executable
os.environ['CHROME_PATH'] = chrome_path
print("Chrome executable path:", chrome_path)
except AttributeError:
print("Shutil does not support which() on this platform.")
except Exception as e:
print(f"An error occurred: {e}")
default_fields = [Gmaps.Fields.NAME, Gmaps.Fields.EMAILS,
Gmaps.Fields.ADDRESS, Gmaps.Fields.ADDRESS]
def jump_lines(count=2):
print("\n" * count)
def get_user_inputs(prompt="Type some text then hit ENTER (type 'done' to finish): "):
# get each config from user
user_inputs = []
while True:
user_input = input(prompt + ":> ")
jump_lines()
if user_input.lower() == '':
break # Exit the loop if the user enters 'done'
else:
user_inputs.append(user_input)
return user_inputs
def get_user_input(prompt):
user_input = input(
prompt + "\n Hit ENTER when you're done \n:> ")
jump_lines()
return user_input
def get_user_number_input(prompt="Type some text then hit ENTER (type 'done' to finish):> "):
# get each config from user
try:
user_input = input(prompt + "\n:> ")
if (user_input != ""):
user_input = int(user_input)
except ValueError:
print("Invalid input. Please enter a whole number.")
return user_input
def scrape_data(queries, **other_config_options):
# run the scrape fucntion given api key and config
if (other_config_options):
Gmaps.places(queries, fields=default_fields, **other_config_options)
else:
Gmaps.places(queries, fields=default_fields)
def save_to_csv(data, output_file='output.csv'):
# save scrape result to csv file
# Data to write to the file
# Write the data to the file "data.json"
bt.write_json(data, "data.json")
# Read the contents of the file "data.json"
print(bt.read_json("data.json"))
# Write the data to the file "data.csv"
bt.write_csv(data, output_file)
# Read the contents of the file "data.csv"
print(bt.read_csv(output_file))
if __name__ == "__main__":
other_config = {}
jump_lines()
print("==========HI 👋 ============")
queries = get_user_inputs(
"PLACES: Search for places (eg: Restaurants in Abuja) then Hit ENTER")
jump_lines()
print("✅ Sucsess:\tSearch Queries Collected Succesfully...")
jump_lines()
print("🎉\Aiit nigga answer a few questions below, \nhint: hit ENTER to accept the default (the value in brackets).")
jump_lines()
max = get_user_number_input("how many records (all)")
jump_lines()
should_scrape_socials = get_user_input(
"should we scrape for socials E-Mail etc.\n yes/no (no)")
jump_lines()
if (should_scrape_socials == "yes"):
YOUR_API_KEY = get_user_input(
"Aiit then, Enter yourr RapiApi key")
other_config["key"] = YOUR_API_KEY
jump_lines()
if (max == ""):
results = scrape_data(queries)
else:
other_config["max"] = max
results = scrape_data(
queries, **other_config)
if results:
print("QUERIES:")
jump_lines()
print(queries)
save_to_csv(results)
jump_lines()
print("🎉 Data successfully saved to 'output.csv' Cheers man 🥂")