scrape from google maps in python

To scrape data from Google Maps in Python, we can use the requests and BeautifulSoup libraries. Here is an example code snippet that scrapes search results for "restaurants near me" in Google Maps:

main.py
import requests
from bs4 import BeautifulSoup

# set up search query
query = "restaurants near me"
url = f"https://www.google.com/maps/search/{query}"

# send GET request to Google Maps
response = requests.get(url)

# parse HTML content with BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")

# find all search results
search_results = soup.find_all("div", {"class": "section-result-text-content"})

# print the name and address of each result
for result in search_results:
    name = result.find("h3").text
    address = result.find("span", {"class": "section-result-location"}).text
    print(name, address)
628 chars
22 lines

This code sends a GET request to the URL for the search query, extracts the HTML content using BeautifulSoup, and parses out the search results using the find_all() method. Then, it loops through each result and prints out the restaurant name and address. Of course, this code can be modified to scrape different types of data from Google Maps or to search for different queries.

gistlibby LogSnag