Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitryzub/271e082691ceb02fa8657a3664635a09 to your computer and use it in GitHub Desktop.
Save dimitryzub/271e082691ceb02fa8657a3664635a09 to your computer and use it in GitHub Desktop.
Scrape all Google Maps Local Results with Python and SerpApi
from serpapi import GoogleSearch
from urllib.parse import urlsplit, parse_qsl
import pandas as pd
import json
params = {
"api_key": "YOUR-API-KEY", # your serpapi api key
"engine": "google_maps", # serpapi search engine
"type": "search", # type of the search. Search, Place..
"q": "Burger", # search query
"hl": "en", # language of the search
"ll": "@47.6080482,-122.3074315,13z" # GPS coordinates of location to search places from
}
search = GoogleSearch(params) # where data extraction happens on the backend
local_place_results = []
page_num = 0
while True:
page_num += 1 # track from which page result comes from
results = search.get_dict() # JSON -> Python dictionary
if not "error" in results:
for result in results.get("local_results"):
print(f"Extracting data from {result.get('title')}")
local_place_results.append({
"page_num": page_num,
"place_name": result.get("title"),
"rating": result.get("rating"),
"reviews": result.get("reviews"),
"place_id": result.get("place_id"),
"address": result.get("address"),
"phone": result.get("phone"),
"website": result.get("website"),
"reviews_link": result.get("reviews_link"),
"photos_link": result.get("photos_link"),
"gps_coordinates": result.get("gps_coordinates"),
"thumbnail": result.get("thumbnail")
# many other types of data
})
else:
print(results["error"] + " Or there's no results to be extracted.")
if "next" in results.get("serpapi_pagination", {}):
search.params_dict.update(dict(parse_qsl(urlsplit(results["serpapi_pagination"]["next"]).query)))
else:
break
# https://stackoverflow.com/a/51634732/15164646
unique_local_place_results = pd.DataFrame(local_place_results).astype(str).drop_duplicates().to_dict("records")
print(json.dumps(local_place_results, indent=2, ensure_ascii=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment