To scrape data from Google Maps using Python
To scrape data from Google Maps using Python, you can use the Google Maps API or a web scraping library such as BeautifulSoup or Scrapy. Here's an example using BeautifulSoup:
1. Install the required libraries:
pip install requests
pip install beautifulsoup4
2. Import the necessary libraries:
import requests
from bs4 import BeautifulSoup
3. Send a GET request to the Google Maps URL:
search_query = "restaurants in New York"
url = f"https://www.google.com/maps/search/{search_query}"
response = requests.get(url)
4. Parse the HTML response using BeautifulSoup:
soup = BeautifulSoup(response.content, "html.parser")
5. Find the specific HTML elements you need:
results = soup.find_all("div", class_="section-result")
6. Extract the desired information from the HTML elements:
for result in results:
name = result.find("h3", class_="section-result-title").text
address = result.find("span", class_="section-result-location").text
phone = result.find("span", class_="section-result-phone-number").text
rating = result.find("span", class_="cards-rating-score").text
print(f"Name: {name}")
print(f"Address: {address}")
print(f"Phone: {phone}")
print(f"Rating: {rating}")
print()
Note: This is a basic example and the specific HTML elements to extract may vary based on the structure of the Google Maps page. Additionally, make sure to respect Google's terms of service and API usage policies when scraping data from Google Maps.
Comments
Post a Comment