Created
June 22, 2022 06:46
-
-
Save dimitryzub/41432bd3d99fe04d047f58113ed8be43 to your computer and use it in GitHub Desktop.
How to scrape all Google Maps Reviews in Python | SerpApi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# video: https://www.youtube.com/watch?v=HQAWQPNjw_k | |
from serpapi import GoogleSearch | |
from urllib.parse import urlsplit, parse_qsl | |
import json | |
params = { | |
"api_key": "your sepapi api key", # your api key | |
"engine": "google_maps_reviews", # serpapi search engine | |
"hl": "en", # language of the search | |
"data_id": "0x549041cdf279071f:0xb4634cde435eb7ec" # place id data located inside Google Maps Place URL: located inside `data=` query parameter. | |
} | |
search = GoogleSearch(params) | |
reviews = [] | |
page_num = 0 | |
while True: | |
page_num += 1 | |
results = search.get_dict() | |
print(f"Extracting reviews from {page_num} page.") | |
if not "error" in results: | |
for result in results.get("reviews", []): # return an empty list [] if no reviews from the place | |
reviews.append({ | |
"page": page_num, | |
"name": result.get("user").get("name"), | |
"link": result.get("user").get("link"), | |
"thumbnail": result.get("user").get("thumbnail"), | |
"rating": result.get("rating"), | |
"date": result.get("date"), | |
"snippet": result.get("snippet"), | |
"images": result.get("images"), | |
"local_guide": result.get("user").get("local_guide"), | |
# other data | |
}) | |
else: | |
print(results["error"]) | |
break | |
if results.get("serpapi_pagination").get("next") and results.get("serpapi_pagination").get("next_page_token"): | |
# split URL in parts as a dict and update search "params" variable to a new page that will be passed to GoogleSearch() | |
search.params_dict.update(dict(parse_qsl(urlsplit(results["serpapi_pagination"]["next"]).query))) | |
else: | |
break | |
print(json.dumps(reviews, indent=2, ensure_ascii=False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment