How to Build a Google Maps Reviews Scraper using Python

Create a Google Maps Reviews Scraper using Python: Extract Users, User Reviews and Star Rating.

Table of Content

Introduction
Why Scrape Google Reviews?
Prerequisites
Step 1: Import the libraries and configure the logging messages
Step 2: Setting Up Playwright
Step 3: Navigating to Google Maps
Step 4: Clean the reviews text function
Step 5: Extracting Reviews
Step 6: Saving Data
Step 7: Running the Script
Best Practices for Scraping
Conclusion 

Introduction

Google Reviews are a critical factor in a business’s online reputation. In this tutorial, we’ll guide you through building a Google Reviews scraper using Python. You’ll learn how to extract and analyze reviews from Google, allowing you to monitor customer feedback and make data-driven decisions for your business.

Why Scrape Google Reviews?

Google Reviews influence how customers perceive businesses online. With millions relying on these reviews to decide where to dine, shop, or stay, collecting and analyzing them can offer key insights into customer satisfaction, trends, and areas needing improvement.

Scraping Google Reviews allows businesses to:

  • Monitor customer feedback in real-time.
  • Identify trends or recurring issues.
  • Analyze competitors’ reviews for benchmarking.

In this guide, we’ll show you how to set up and execute a Google Reviews scraper using Python and Playwright, a powerful library for automating browsers.

Prerequisites

Before diving into the code, make sure you have the following tools and libraries installed:

  1. Python 3.8 or higher
  2. Node.js (required for Playwright installation)
  3. The following Python libraries: playwright, pandas, bs4 (BeautifulSoup for HTML parsing), emoji (to clean the emoji in the reviews), and logging.

Install these libraries using pip:

pip install playwright pandas emoji

Note: Initialize Playwright with the following command to download the necessary browser binaries:

playwright install

Step 1: Import the libraries and configure the logging messages

from playwright.sync_api import sync_playwright
import pandas as pd
import re
import emoji
import logging
import time

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

Step 2: Setting Up Playwright

Playwright is a Python library for automating browsers. Unlike Selenium, Playwright is faster and offers more advanced features like built-in proxy support and better handling of dynamic content.

To get started, import the necessary Playwright modules and set up a browser instance:

def initialize_browser():
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    return playwright, browser, page

This function initializes a Chromium browser and opens a new page for interaction.

Step 3: Navigating to Google Maps

To scrape Google Reviews, you need to search for a business on Google Maps and navigate to its reviews section. Use the following function:

def search_google_maps(page, business_name):
    page.goto("https://www.google.com/maps")
    search_box = page.locator("input[id='searchboxinput']")
    search_box.fill(business_name)
    search_box.press("Enter")
    page.wait_for_timeout(5000)

Step 4: Clean the reviews text function

Clean text by removing emojis and extra whitespace..

def clean_text(text):
    # Remove emojis
    text = emoji.replace_emoji(text, replace='')
    
    # Remove extra whitespace
    text = re.sub(r's+', ' ', text).strip()
    
    return text

Step 5: Extracting Reviews

Google dynamically loads reviews, so scrolling is necessary to load additional content. Playwright handles this effectively:

def scrape_reviews(page, max_reviews=60):
    reviews = []
    try:
        # Wait for the business details to load
        page.wait_for_timeout(5000)
        
        # Locate and click the reviews section
        logger.info("Searching for reviews section")
        review_section = page.get_by_role('tab', name="Reviews")
        review_section.click()
        page.wait_for_timeout(3000)

        # Scroll to load more reviews
        logger.info("Loading reviews...")
        for _ in range(10):
            page.mouse.wheel(0, 5000)
            page.wait_for_timeout(2000)

        # Extract reviews
        review_elements = page.locator("div[class*='jJc9Ad']")
        logger.info(f"Found {review_elements.count()} reviews")

        for element in review_elements.all()[:max_reviews]:
            reviewer = element.locator("div[class*='d4r55']").inner_text()
            rating = element.locator("span[aria-label]").get_attribute("aria-label")
            review_text = element.locator("span[class*='wiI7pd']").inner_text()

            reviews.append({
                "Reviewer": clean_text(reviewer),
                "Rating": rating,
                "Review": clean_text(review_text)
            })
        
    except Exception as e:
        logger.error(f"Error during scraping: {e}")
    
    return reviews

Step 6: Saving Data

Once the reviews are scraped, save them in a structured format like CSV for further analysis:

def save_reviews_to_csv(reviews, filename="google_reviews.csv"):
    df = pd.DataFrame(reviews)
    df.to_csv(filename, index=False, encoding='utf-8')
    logger.info(f"Reviews saved to {filename}")

Replace the filename according to your preferences.

Step 7: Running the Script

Finally, tie everything together in a main function:

def main():
    business_name = "Panzerotti Luini"
    
    # Initialize browser
    playwright, browser, page = initialize_browser()
    
    try:
        # Search and scrape reviews
        search_google_maps(page, business_name)
        reviews = scrape_reviews(page, max_reviews=200)
        
        # Save results
        save_reviews_to_csv(reviews)
    
    except Exception as e:
        logger.error(f"Unexpected error: {e}")
    
    finally:
        # Add a longer wait before closing
        page.wait_for_timeout(5000)
        browser.close()
        playwright.stop()

if __name__ == "__main__":
    main()

Note: Replace the business_name with the business you want to scrape.

Once you run the scraper, a csv file will be created in your directory. Here’s the example:

Reviewer,Rating,Review
H L,5 stars,Delicious! They have daily specials and seasonal flavors. Came in November and their savory special was a pesto and mozzarella and the sweet savory was a chestnut and almond. Got the seasonal sweet pastry and a classic ham and mozzarella. …
Oksana Vishnevskaia,4 stars,"There's usually a long line here, but it moves very quickly. I really enjoyed the savory pastries, especially the ones with ham and salami. …"
turki chayma,5 stars,"You can only say WAW about a such place . Big respect for the places where they serve everything fresh and hot. For me it’s a must!!! You should try the classic fried and baked, bothhhh!!! then choose any other …"
Yu-Ting,3 stars,Tried the spicy salami and cheese. It was a good walking snack. The dough was lightly crisp on the outside and fluffy on the inside. The salami and cheese were warm and not hot which was a little disappointing.
Thanath Gopalan,5 stars,There was a queue but it moved really fast. Had the mozzarella and ricotta cheese. Was really good. Absolutely worth it.
Matthew Garcia,5 stars,"Not much I can add here as it’s already extremely popular but it’s justified! Very efficient operation and guaranteed fresh product because it’s so busy. Savory and sweet flavors for everyone. Quick, easy, delicious, authentic. This must be what real “fast food” is in Milan. A must-visit!"
都娟,3 stars,"A long queue. The girls have no smiles, no time to explain. Just order and leave, half a minute. Ordered one fried with tomato and mozzarella, one baked with spinach. Don’t expect too much, just exactly the same as pizza."
Reza Nobahar,4 stars,Does it worth the wait? It definitely did to try something Milan famous for and it’s part of local experience however nothing too exciting or super amazing that make you go back there everyday …
Dr Toey,4 stars,I was expecting more after the reviews but the panzerotti was not that fresh and crispy. Looks like it was cooked a while ago before putting in the display.
Irmak Yılmaz,5 stars,I waited like 15 minutes. (I arrived at 12.00). I tried tometo+mozzarella+pesto (panzerotti of the day) one and the ricotta+spinach one. They were sooo good. I loved them both. It was fried and the ingredients were incredibly good and …
Lu ca,5 stars,little pizza doughnuts filled with goodness! so good if you go early or late in the day the queues are short! lunchtime there is about a 15min wait.
Sangeeta Chandra,4 stars,"Good panzerotti, quick snack to grab. Ordered the spicy salami and mozzarella which was good. This one was deep fried, so one would be more than enough. Would recommend sharing if you prefer a lighter snack. They do have baked options as well."
J B,5 stars,"Having never had Panzerotti, we didn’t really know what to expect, but since this place has been in business since 1888, you know they are doing everything right! We were in Italy for a total of two weeks, and these Panzerotti have to be …"
Muhammet Ikbal Karatas (Alumni),3 stars,"Cannolo we tried wasn’t fresh, it was soft and the cream wasn’t as good as the cannolis we tried elsewhere. …"
Idalécio Rosa,5 stars,Beautiful place. Delicious Italian panzerotti in Milan just next to Duomo. Please be ready to queue it's a busy place but with quick service. I really recommend it!
Dodd Ouyang,5 stars,hungry? don't want spontini? saw a huge line-up and afraid it's gonna take half a day? …
Bianca Allery,3 stars,Fairly ok. I’m not quite sure why it’s so popular as it wasn’t anything to write home about. The 4 cheeses one was the best! The baked spinach and ricotta is a no-go. The ham and mozzarella was ok.
Itsjustnathalie,4 stars,"I went twice, once during the week and once on a Saturday. During the week it was absolutely amazing & delicious and I was very excited to come back. On Saturday though I was a little disappointed, my panzerotto nearly didn’t have any …"
Anna,5 stars,When we saw the line we were afraid that this place is just another tourist trap. The truth is that the panzerotti are absolutely amazing. We came twice during our stay. …
Seher Baysal,3 stars,Panzerotti was ok but pizza wasn’t good. And please DO NOT EAT ANY DESSERT here. I’ve tried a few different type of dessert and I didn’t like it at all.
Edwin Labja,5 stars,"If you find yourself wandering the vibrant streets of Milan, make sure to join the buzz at Luini, right by Piazza Duomo. Known for its heavenly panzerotti, this little bakery has been a beloved Milanese institution since 1888, and it’s …"
Rana,5 stars,"By far the best thing I’ve ever eaten in my life. We tried the pistachio, ricotta and chocolate, and the best one, mozzarella and tomato. Went back so many times for more and it was still delicious!"
Nuha Khalil,5 stars,I tried Luini panzerotti with mozzarella and pomodoro it was extremely delicious the dough itself is totally different so fluffy and light and tasty The only problem is that it's small …
argiro mpn,5 stars,"We found the place just as it was opening, and there was only a small line outside, so we waited. The Panzerotti were warm, very tasty, and reasonably priced."
Dhanuka Perera,5 stars,"Tried their fried panzerotti. Task is amazing and and must have food in Milan. Lot of varieties choose from, tried the onion and olive one."
Ehab Badawy,4 stars,One of the best places in Italy to have fried Panzerotti in the world. They are super fresh. The dough is very light. I always thought that it would be oily when fried but to my surprise it was not. Just it is good to give you the taste you are looking for without being oily. We love it. You should try it when you’re in Milan.
Rason Chan,5 stars,"Long line. Really long line. I had two creatures who were chatting with the guy in front of me pretended that they know each other and then cut in line. When I asked what they were doing, they just played dumb, saying they needed to go to this or that place—total excuses. Watch out for animals like that."
Lana Figueroa,3 stars,My husband liked the cheese and tomato sauce. I liked the ham and cheese but abit too oily for me. The lady wasn’t very nice I was trying to say in Italian which I spoke fine in but she seemed annoyed and there was no big line it was not necessary.
Andrei Jaume Willenska,5 stars,"I had never tried a panzerotti, but I checked this place out in the middle of downtown Milan and it did not disappoint. I ate the mozzarella and salami picante panzerotti and man, this thing was delicious. There was a bit of a line, but it …"
Stacee Kersley,5 stars,"Quick drop into this little spot while we waited for our timed entry to the Duomo and what a find! We've had other panzerotti before that were good, but greasy. These were great because we had the option of fried or baked. We only had …"
Zuzanna Tulej,5 stars,"Very good panzerotti, a great choice for a quick and cheap snack. 3€ per piece, a good selection of vegetarian options. It’s quite fatty, but also delicious. I really recommend this place! …"
Jimkaf_07,5 stars,"Amazing panzerotti , really tasty , exactly what you need to start the day off right in Milan ! Make sure to get there early , because they sell out fast ! 10/10"
Fanzhu Deng,5 stars,"Was recommended by my friend to try some Italian street food. Ordered mozzarela with tomatoes. Completely blew my mind. Very busy place, service was quick, which we had more time to ask about the menu and try more flavours."
merel swinkels,5 stars,"Very quick service, there is a queue. The food is very delicious and also very cheap. It is 3 euros for a panzerotti! I would definitely recommended if you are close to the duomo and want a quick bite."
Daiana G,5 stars,"Great place of Panzerotti! 3€ each, the original one is fried with mozzarella and pomodoro. There's always queue but it goes fastttt (italian pronunciation xd)"
Jun Ren Ong,5 stars,"Long queue but it moves fast. I got the fried panzerotti (essientially fried dough with ingredients), taste pretty good. Recommene to try if you are in the area. I got the seasonal special 4 cheese flavour."
Lorenzo A. G,5 stars,What a place! One the best street food you can find in Milan! Panzerotto with tomato and mozzarella simply perfect.
Mateusz Nowak,5 stars,"panzerotti is simply heaven in your mouth. delicious, really delicious."
Abhirup Bose,5 stars,"Tucked away in a tiny lane next to the Duomo, it’s a very tasty food. Waiting in line is absolutely worth the food you get. Besides, the line moves pretty fast so it’s not a long wait. It’s inexpensive and very tasty food. We had a fried …"
Asianfoodninja,5 stars,"Popular street food in Milan The queue can be really long and busy, lines start before they open. …"
Nicola Glaasford,5 stars,Amazing sandwich... Shaped like a calzone but the dough was more bread-like and a little sweet... We got the prosciutto and cheese and hot salami and cheese and both were fantastic.... heard about the big queues but I guess we got lucky cos …
Wafa azzam,3 stars,"It is okay, but if your stomach is sensitive; never choose the fried one."
Lindy L,4 stars,"There was a massive queue when we went, but the wait wasn't too long. We tried 4 different types of Panzerotti's. …"
Sneha Naran,4 stars,Quick and fast service. We tried the spinach and ricotta and tomato onion and olives. Both good but the latter better.
Gohar Jamal,5 stars,"Panzerotti Luini near Duomo in Milan is a must-visit! The panzerotti are deliciously crispy and filled with rich, flavorful ingredients. The staff is friendly, and the location is perfect for a quick, satisfying meal after sightseeing. Highly recommend!"
Rodanthi Lala,5 stars,Great food. Try focaccia and panzerotti.
Anita fari,3 stars,"The panzerotti was delicious, staff not so happy"
Mariján Róka,5 stars,When I was a child in my hometown we had a place where Panzerotti was available. So roughly 35 years later I was able to try again this delicious food and after …
Nandita V,5 stars,Super delicious savory snack! It's quite famous for their long standing presence and rightly so. The …
John Mauro,5 stars,"I know this place since I was little l, and now I don’t live in Italy anymore. But every time I come in Milano to visit my friends and family, Luini is always a must stop for me. …"
Midhun Ogirala,5 stars,The panzerotti here is my favourite. Other items here are good as well. The queue is very big usually but moves fast . If there was dine in it would've been great.
Melvrick Goh,5 stars,Loved the sweet panzerotti’s. Outstanding. Try everything if we could have. The orange cream cheese cannoli was bomb …
Elton Donnel Cabardo (Tonnex),5 stars,"Worth the hype! The panzerottis are cheap and absolutely delicious. Don’t get intimidated by the line, It moves really fast."
Zubaer Ahmad,4 stars,Super delicious Italian snacks. Really liked the tomato cheese one. Also very affordable. My suggestion is go for the fried version rather than the baked one.
Seema Elremmash,5 stars,This fried pocket was my favorite thing I’ve had on my whole trip to Italy. I love that they are fresh too.
Khaled Saif,4 stars,"The mozzarella and tomato is definitely the best choice here – fresh, flavorful, and a perfect balance of ingredients. However, be prepared to eat while standing since there are no seats or tables available. The place tends to get a bit crowded with some traffic and a queue, but the food is worth the wait!"
khaled moujahed,4 stars,"Before my visit to Milan, everyone recommended that I try a restaurant with an Italian specialty that can’t be found elsewhere. So, I decided to discover this panzerotti, and I absolutely loved it. It reminded me of Tunisian fricassée, but with a different filling. I highly recommend you try it!"
Muskan Harpalani,3 stars,Overrated place in Milan….its tasty but its not worth the hype…cheap snack in Milan.
Jordan Glick,5 stars,The pepperoni and cheese rolls are next level good. Highly recommend coming early to avoid the wait.
Muriel Baumeister,5 stars,"When we arrived there were a lot of traffic. We took a baked Panzerotti with tomato and Mozzarella. It tasted really good . We definitely will come back when we are in beautiful Milano again. Quick service, the food is for take away. …"

Best Practices for Scraping

  1. Add Delays Between Requests

Websites often monitor the frequency of requests to prevent scraping. Adding small delays between actions can mimic human behavior and reduce the risk of getting blocked. In our script, you can adjust the page.wait_for_timeout() calls for this purpose.

page.wait_for_timeout(3000) # 3-second delay

2. Use Proxy Rotation

For scraping a large number of reviews, especially across multiple businesses, proxy rotation is essential. It ensures your requests originate from different IPs, avoiding detection and blocking. Rayobyte offers reliable proxy services.

Proxy Rotation Setup with Rayobyte.

Below is a script to integrate proxies into the Playwright setup:

def initialize_browser_with_proxy(proxy_url):
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(
        headless=False, 
        proxy={"server": proxy_url}
    )
    context = browser.new_context()
    page = context.new_page()
    return playwright, browser, page

def main():
    # Example usage
    proxy_list = ["http://username:[email protected]:1234", 
              "http://username:[email protected]:1234"]

    business_name = "Panzerotti Luini"
    
    for proxy in proxy_list:
        playwright, browser, page = initialize_browser_with_proxy(proxy)
        try:
            search_google_maps(page, business_name)
            reviews = scrape_reviews(page, max_reviews=100)
            save_reviews_to_csv(reviews, filename=f"reviews_with_{proxy.split('@')[1]}.csv")
        finally:
            browser.close()
            playwright.stop()

if __name__ == "__main__":
    main()

3. Respect Website Terms of Service

Before scraping, always check the website’s Terms of Service. Use the data responsibly and ensure compliance with local laws and regulations.

Conclusion

With this Google Reviews scraper, you can efficiently gather customer feedback and analyze it for actionable insights. Using Playwright ensures smooth handling of dynamic content, making the scraping process seamless and efficient.

This tutorial offers a foundation—extend it to scrape other details or integrate with analytics tools for deeper insights.

If you encounter any issues or have questions while following this tutorial, feel free to leave a comment below. I’d be happy to help you troubleshoot and provide additional guidance!

Responses

Related Projects

New Bing Screenshot
b9929b09 167f 4365 9087 fddf3278a679
DALL·E 2024 12 05 18.44.15 A visually appealing banner image for a blog titled Scrape Google Trends Data Using Python. The image features a laptop displaying Google Trends on
tiktok information downloader