This script automates data scraping from Google Maps using Selenium. It performs searches based on user-provided queries, extracts detailed location information (name, coordinates, ratings, reviews, contact details, and more), and appends this data to a CSV file for further analysis.
- Automates searching and data extraction from Google Maps
- Saves location data into a single CSV file named with a timestamp
- Handles errors gracefully if some data fields are unavailable
- Uses a configuration file (
config.json) for query customization
Before running the script, ensure the following are installed:
- Python 3.7+
- Google Chrome
- ChromeDriver (compatible with your version of Chrome)
- Install Selenium package
# Clone or download the repository
# Install required Python libraries
pip install selenium
# Download ChromeDriver
# Ensure ChromeDriver matches your Chrome version
# Add ChromeDriver to your PATH-
Set up
config.json:Create a file named
config.jsonin the root folder. Define your search queries as follows:{ "queries": [ "restaurants near me", "cafes in New York", "hotels in Paris" ] } -
Run the script:
python script_name.py
-
Access the output:
The results will be saved in the
outputfolder with a filename likescrap-YYYY-MM-DD_HH-MM-SS.csv.
Usage tip: As Google Maps limits the results per query, I strongly suggest using specific queries in the config.json file. For instance, specify query per state instead of a country.
- Initialization: The script creates an output folder and a CSV file with headers for storing location data.
- Configuration Loading: Reads search queries from
config.json. - Google Maps Automation:
- Launches Google Maps in a headless Chrome browser (or visible window for debugging)
- Performs searches using the provided queries
- Data Extraction: For each location result:
- Extracts data like name, coordinates, ratings, reviews, address, phone, and website
- Handles missing fields with fallback mechanisms
- Appends extracted data to the CSV file
- Cleanup: Closes the browser after processing all queries
load_config: Reads and parses theconfig.jsonfileopen_google_maps: Initializes the Chrome WebDriver and opens Google Mapssearch_query: Searches Google Maps using a provided queryget_place_data: Extracts details about a specific locationclick_all_elements: Iteratively clicks on location elements and triggers data extractionappend_to_csv: Appends data to the CSV file
The output is a CSV file saved in the output folder. The file includes the following fields:
name: Name of the placeurl: URL of the Google Maps page for the placelatitude: Latitude of the locationlongitude: Longitude of the locationaverage_rating: Average review rating (if available)review_count: Number of reviews (if available)address: Address of the location (if available)phone: Phone number (if available)website: Website URL (if available)
- Ensure ChromeDriver is in your PATH and matches your Chrome version
- If elements are not found, verify that Google Maps has not changed its DOM structure. Update selectors as needed
- If the browser doesn't open or work properly, confirm that dependencies are installed correctly
For support or contributions, you can contact me at [email protected]