Last active
June 22, 2022 17:12
-
-
Save dimitryzub/aa5a26c41620a3f1174326060d376954 to your computer and use it in GitHub Desktop.
Web Scrape Google Maps Photos with Python and 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=XM9d8zTYp_U | |
from serpapi import GoogleSearch | |
from urllib.parse import urlsplit, parse_qsl | |
import json | |
params = { | |
"api_key": "serpapi api key", # your api key | |
"engine": "google_maps_photos", # 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) | |
photos = [] | |
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("photos", []): # return an empty list [] if no photos from the place | |
photos.append({ | |
"page": page_num, | |
"image": result.get("image"), | |
"thumbnail": result.get("thumbnail") | |
}) | |
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(photos, indent=2, ensure_ascii=False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment