-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_google_reviews.py
More file actions
400 lines (323 loc) · 13.8 KB
/
scrape_google_reviews.py
File metadata and controls
400 lines (323 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
""" This script scrapes Google Maps Reviews.
https://levelup.gitconnected.com/web-scrape-google-maps-reviews-with-playwright-for-free-7d6f42f1719d"""
import time
import os
import asyncio
import json
import pandas as pd
from playwright.async_api import async_playwright
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from os import path
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import random
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
def read_json_as_dataframe(file_path):
"""
Reads a JSON file from the given path and returns it as a pandas DataFrame.
Args:
path (str): The file path to the JSON file.
Returns:
pd.DataFrame: DataFrame containing the JSON data.
"""
data = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
# Parse each JSON object and append to the list
data.append(json.loads(line))
# Convert the list of JSON objects to a DataFrame
df = pd.DataFrame(data)
return df
async def check_json(response, output_path):
""" This function is triggered when we get a response from the
network and tackles the response containing 'listugcposts'.
The text is converted to json, and the data is obtained. """
if "listugcposts" in response.url:
content = await response.text()
content = content.split(")]}'\n")[1]
# Parse the string as JSON
data_raw = json.loads(content)
data_raw = data_raw[2]
def safe_extract(data, *path, default=None):
try:
for key in path:
data = data[key]
return data
except (IndexError, KeyError, TypeError):
return default
for i, element in enumerate(data_raw):
timestamp = data_raw[i][0][1][6]
# if 'year' not in timestamp and 'years' not in timestamp:
print (data_raw)
data = {
'review': safe_extract(
data_raw, i, 0, 2, 15, 0, 0),
'rating': safe_extract(
data_raw, i, 0, 2, 0, 0),
'source': safe_extract(
data_raw, i, 0, 1, 13, 0),
'timestamp': safe_extract(
data_raw, i, 0, 1, 6),
'language': safe_extract(
data_raw, i, 0, 2, 14, 0),
}
# i
# time stap 0 > 0 > 0 > 1 > 6
# Load existing data into a set to avoid duplicates
existing_data = set()
if os.path.exists(output_path):
with open(output_path, 'r', encoding='utf-8') as file:
for line in file:
existing_data.add(line.strip())
new_data_json = json.dumps(
data, ensure_ascii=False)
if new_data_json not in existing_data:
# Write the new data to the JSON file
with open(output_path, 'a', encoding='utf-8') as file:
file.write(new_data_json + '\n')
async def main_google_reviews(search_input):
""" This is the main function to scrape Google Reviews """
# google's main URL
# url = (
# "https://www.google.com/maps/@38.7156642,-9.1243907,16z?entry=ttu")
#Chiang Mai
url = (
"https://www.google.com/maps/@18.7943903,98.8740742,16z?entry=ttu")
#Koh Phangan
url = (
"https://www.google.com/maps/@9.7563241,99.9631365,16z?entry=ttu")
# file path
path = f"reviews_google_{search_input}__{timestamp}.json"
async with async_playwright() as pw:
# creates an instance of the Chromium browser and launches it
browser = await pw.chromium.launch(headless=False)
# creates a new browser page (tab) within the browser instance
page = await browser.new_page()
# go to url with Playwright page element
await page.goto(url)
await page.mouse.wheel(0, 35000)
# avoid cookies
# page.locator("text='Reject all'").first.click()
page.locator("text='Rifiuta tutto'").first.click()
# write what you're looking for
time.sleep(1)
await page.fill("#searchboxinput", search_input)
time.sleep(2)
# press enter
await page.locator("#searchboxinput").click()
time.sleep(1)
# select the first element in the list of options
await page.keyboard.press('ArrowDown')
await page.keyboard.press('Enter')
# get tab with the reviews
#await page.locator("text='Reviews'").first.click()
await page.locator("text='Recensioni'").first.click()
time.sleep(1)
await page.mouse.wheel(0, 35000)
time.sleep(1)
# search for most recent
#await page.locator("text='Sort'").first.click()
await page.locator("text='Ordina'").first.click()
time.sleep(1)
#await page.locator("text='Valutazione più bassa'").first.click()
await page.keyboard.press('ArrowDown') # newest
time.sleep(1)
await page.keyboard.press('ArrowDown') #highest rating
time.sleep(1)
await page.keyboard.press('ArrowDown') #lowest rating
time.sleep(1)
await page.keyboard.press('Enter')
time.sleep(1)
n=4
for i in range(n):
await page.mouse.wheel(0, 35000)
# get a response
page.on(
"response",
lambda response: asyncio.create_task(
check_json(response, path)))
print(f'{i+1}/{n} scrolling')
time.sleep(1)
# save to a csv file
df = read_json_as_dataframe(path)
df.to_csv(f'data/reviews_{search_input}.csv', index=False)
time.sleep(1)
await page.close(run_before_unload=True)
time.sleep(1)
# remove json file
# os.remove(path)
# Function to convert the timestamp to the correct date
def convert_timestamp_to_date(timestamp):
"""
Converts a timestamp string to a datetime object.
Adapt the strings to your own language !
Args:
timestamp (str): A string representing the timestamp.
Returns:
datetime: The corresponding datetime object.
"""
now = datetime.now()
if 'un anno fa' in timestamp:
# Extract the number of years (1) and subtract it from the current date
years_ago = 1
date = now - relativedelta(years=years_ago)
elif 'anni fa' in timestamp:
# Extract the number of years and subtract it from the current date
years_ago = int(timestamp.split()[0])
date = now - relativedelta(years=years_ago)
elif 'settimane fa' in timestamp :
# Extract the number of months and subtract it from the current date
weeks_ago = int(timestamp.split()[0])
date = now - relativedelta(weeks=weeks_ago)
elif 'settimana fa' in timestamp:
# Extract the number of months and subtract it from the current date
weeks_ago = 1
date = now - relativedelta(weeks=weeks_ago)
elif 'mese fa' in timestamp or 'mesi fa' in timestamp:
# Extract the number of months and subtract it from the current date
months_ago = int(timestamp.split()[0])
date = now - relativedelta(months=months_ago)
else:
# Default case if the format doesn't match (you can customize this)
date = now
return date
def grey_color_func(word, font_size, position, orientation, random_state=None,
**kwargs):
"""
Generates a color for a word based on its position and font size, using a grey scale.
Args:
word (str): The word to be colored.
font_size (int): The font size of the word.
position (tuple): The (x, y) position of the word in the visualization.
orientation (str): The orientation of the word (e.g., horizontal, vertical).
random_state (int, optional): Seed for the random number generator (default is None).
**kwargs: Additional keyword arguments.
Returns:
str: A color in HSL (Hue, Saturation, Lightness) format with varying lightness for grey scale.
"""
return "hsl(33, 73%%, %d%%)" % random.randint(60, 100)
def wordcloud(df):
"""
Reads the text in column 'review' where the language is 'en' and makes a wordcloud of it
Args:
df (pd.DataFrame): The DataFrame containing the data with a 'review' (and 'language') column.
Result:
Wordcloud in svg and png format
"""
# Read the whole text.
# Filter the rows where 'language' is 'en' and concatenate the reviews
text = " ".join(df[df['language'] == 'en']['review'].dropna().astype(str))
# adding movie script specific stopwords
stopwords = set(STOPWORDS)
stopword_list = ["und", "sehr", "die", "da", "ist"]
for s in stopword_list:
stopwords.add(s)
wc = WordCloud(max_words=100, stopwords=stopwords, margin=10,
random_state=1).generate(text)
# store default colored image
default_colors = wc.to_array()
plt.title("Wordcloud")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
interpolation="bilinear")
# PNG
wc.to_file(f"wordcloud_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
# SVG
# https://stackoverflow.com/questions/44715044/how-to-pass-a-python-wordcloud-element-to-svgwrite-method-to-generate-a-svg-of-t
wordcloud_svg = wc.to_svg(embed_font=True)
f = open(f"wordcloud_{datetime.now().strftime('%Y%m%d_%H%M%S')}.svg","w+")
f.write(wordcloud_svg )
f.close()
plt.axis("off")
plt.show()
def update_column_names(df):
"""
Updates column names by replacing 'x' with 'abs' and 'y' with 'perc'.
Args:
df (pd.DataFrame): The DataFrame whose column names need to be updated.
Returns:
pd.DataFrame: The DataFrame with updated column names.
"""
# Create a dictionary for renaming columns
rename_dict = {col: col.replace('x', 'abs').replace('y', 'perc') for col in df.columns}
# Rename the columns
df = df.rename(columns=rename_dict)
return df
def calculate_ratings_and_averages_per_period(df, index_column):
"""
Calculates both the number of ratings per year and the average rating per year,
then combines these metrics into a single DataFrame and prints the result.
Args:
df (pd.DataFrame): The DataFrame containing the data with 'year' and 'rating' columns.
Returns:
pd.DataFrame: A DataFrame containing the number of ratings and the average rating per year.
"""
# Calculate the number of ratings per year
ratings_per_period = df.groupby(index_column)['rating'].count().reset_index().rename(columns={'rating': 'number_of_ratings'})
# Calculate the average rating per year
average_rating_by_year = df.groupby(index_column)['rating'].mean().round(2).reset_index().rename(columns={'rating': 'average_rating'})
# Merge the two results into a single DataFrame
combined_table = pd.merge(ratings_per_period, average_rating_by_year, on=index_column)
combined_table = update_column_names(combined_table)
# Print the result
print(f"Combined ratings and averages per {index_column}:")
print(combined_table)
def calculate_rating_distribution(df, index_column):
"""
Calculates the distribution of ratings (absolute counts and percentages) per year and prints the results.
Args:
df (pd.DataFrame): The DataFrame containing the data with a 'year' column.
index_column : 'year' | 'month'
Returns:
None
"""
rating_distribution = df.pivot_table(
index=index_column,
columns='rating',
aggfunc='size',
fill_value=0
)
rating_distribution = rating_distribution.reindex(columns=[1, 2, 3, 4, 5], fill_value=0)
rating_distribution_perc = round(rating_distribution.div(rating_distribution.sum(axis=1), axis=0) * 100, 2)
# Add a 'total' column with the sum of ratings for each row
rating_distribution['total'] = rating_distribution.sum(axis=1)
combined_table = pd.merge(rating_distribution, rating_distribution_perc, on=index_column)
combined_table = update_column_names(combined_table)
# Print the result
print(f"Ratings distributions, absolute and percentage, per {index_column}:")
print(combined_table)
def analyse(path):
"""
Performs a comprehensive analysis of the data at the given path and prints various metrics.
Args:
path (str): The file path to the JSON data.
Returns:
None
"""
df = read_json_as_dataframe(path)
df['date'] = df['timestamp'].apply(convert_timestamp_to_date)
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.to_period('M')
print (df)
calculate_ratings_and_averages_per_period(df, 'year')
calculate_rating_distribution(df, 'year')
#df = df[df['date'] >= (datetime.now() - relativedelta(months=12))].copy()
calculate_ratings_and_averages_per_period(df, 'month')
calculate_rating_distribution(df, 'month')
wordcloud(df)
def main():
#RETRIEVE
# SEARCH_INPUT = "Goodsouls Kitchen - Vegan Restaurant"
SEARCH_INPUT = "Kia Ora Café"
SEARCH_INPUT = "Gummy Bear Restaurant Thai food and Vegan food"
asyncio.run(main_google_reviews(SEARCH_INPUT))
# ANALYSE
#path = f"reviews_google_{SEARCH_INPUT}.json"
#path = f"reviews_google_goodsouls_kitchen_{timestamp}.json"
#path = f"reviews_google_Kia Ora Café_{timestamp}.json"
path = f"reviews_google_{SEARCH_INPUT}_{timestamp}.json"
path = "reviews_google_Gummy Bear Restaurant Thai food and Vegan food__20240818_230759.json"
# path = "reviews_google_Kia Ora Café__20240818_225641.json"
analyse(path)
if __name__ == '__main__':
main()