Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search businesses in different areas on Google Maps
import requests
import json
import csv
MAPS_KEY='YOUR_API_KEY' # Get API_key & Enable Places_API on GCP
MAPS_RADIUS='3000' #3KM radius
MAPS_KEYWORD='PHYSIOTHERAPY' # What are you looking for
CITIES = [
{'name': 'madrid', 'cord': '40.416586, -3.703884'},
{'name': 'madrid_south', 'cord': '40.410153, -3.705196'},
{'name': 'madrid_north', 'cord': '40.428533, -3.704031'},
]
f=csv.writer(open('./export.csv','w', newline='', encoding='utf-8'))
for city in CITIES:
URL = str('https://maps.googleapis.com/maps/api/place/nearbysearch/json?key='+ MAPS_KEY +'&location='+ city['cord'] +'&radius='+ MAPS_RADIUS +'&keyword='+ MAPS_KEYWORD)
req = requests.get(URL)
resp = json.loads(req.text.encode('utf8'))
count = 0
for result in resp['results']:
z = list()
url_data = str('https://maps.googleapis.com/maps/api/place/details/json?place_id='+ result['reference'] +'&fields=formatted_phone_number,website&key='+ MAPS_KEY)
for k, v in result.items():
if 'name' in k:
z.append(v)
if 'code' in k:
z.append(city['name'].replace('_',' '))
z.append(v['global_code'])
if 'rating' == k:
z.append(v)
if 'types' in k:
z.append(",".join(v))
if 'total' in k:
z.append(v)
if 'vicinity' in k:
z.append(v)
data = json.loads(requests.get(url_data).text.encode('utf8'))
z.append(str(data['result'].get('formatted_phone_number','')))
z.append(str(data['result'].get('website','')))
f.writerow(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment