So this is just an update to the previous script. Rather than advancing in a straight line, x and y range loops have been added to get everything in your proximity within a range. This becomes exponentially bigger, of course, so best to start out with a tiny range. With a distance of 2 I scraped nearly 100 businesses.

Hope you find it useful as well

import http.client
import json
import csv

api_key = 'AIpUtY0uR4pIK3yheREmOthaF3R'
lat = 40.730610
lng = -73.935242

def getPlaces(lat, lng):
  conn = http.client.HTTPSConnection("places.googleapis.com")
  payload = json.dumps({
    "includedTypes": [],
    "maxResultCount": 20,
    "locationRestriction": {
      "circle": {
        "center": {
          "latitude": lat,
          "longitude": lng
        },
        "radius": 1000
      }
    }
  })
  headers = {
    'X-Goog-Api-Key': api_key,
    'X-Goog-FieldMask': 'places.displayName,places.websiteUri,places.id,places.googleMapsUri',
    'Content-Type': 'application/json'
  }
  conn.request("POST", "/v1/places:searchNearby", payload, headers)
  res = conn.getresponse()
  data = res.read()
  return json.loads(data.decode("utf-8"))

places_by_id = {}

distance = 2
for x in range (-distance, distance):
  lat = 33.785663524 + (x * 0.01)

  for y in range (-distance, distance):
    lng = -116.9666628 + (y * 0.01)

    parsed_data = getPlaces(lat, lng)
    places = parsed_data.get('places')

    if places:
      for place in parsed_data.get('places'):
          places_by_id[place['id']] = {
              'id': place['id'],
              'googleMaps': place['googleMapsUri'],
              'text': place['displayName']['text'],
              'website': place.get('websiteUri'),
          }

for place in places_by_id:
    print(places_by_id[place])

with open('places.csv', 'w', newline='') as csvfile:
    fieldnames = ['id', 'googleMaps', 'text', 'website']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for place in places_by_id:
        writer.writerow(places_by_id[place])

By rmarin

Leave a Reply

Your email address will not be published. Required fields are marked *