Harnessing Recursion to Retrieve all Reviews.

A Google My Business Example with Python

Photo by Laurie Byrne on Unsplash

Obtaining Tokens and Ids

# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
headers = {
'Authorization':
'Bearer 1hsdf0983223ladf.'xcvlwqr0i3098dfglm34eoj234rkjdfsav-90fdgfgfjfdlgkja'}

Obtaining Reviews by Recursion

Retrieve the first page of reviews:

pip install requests import requests# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
URI = f'https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews'r = requests.get(URI, headers=headers)
print(f'status code: {r.status_code}')

result = r.json()
result
Response body for the review list request.
Quer Parameters for locations reviews list
URI = f'https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews?pageToken='

Retrieving all reviews with recursion

import pandas 
import time
import requests
# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
URI = f'<https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews>'
# Automate data extraction using recursive function:
def reviews_to_df(headers, accountId, locationId, df, URI):
r = requests.get(URI, headers=headers)
print(f'status code: {r.status_code}')
time.sleep(3)
result = r.json()
new_df = df.append(result['reviews'])
print(f'the shape of the df is now: {new_df.shape}')
if len(result['reviews']) != 50:
return new_df
else:
nextPage = result['nextPageToken']
URI = f'<https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews?pageToken={nextPage}>'
return reviews_to_df(headers, accountId, locationId, new_df, URI)
empty_df = pd.DataFrame()
df = reviews_to_df(headers, accountId, locationId, empty_df, URI)

Explanation:

Conclusion

References

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store