如何使用 Python 从谷歌地图抓取数据?

pythonserver side programmingprogramming

谷歌地图是一个强大的工具,它提供了大量的地理空间数据,包括位置、地址、评论、评分等。能够以编程方式提取这些数据对于各种应用(如业务分析、研究和数据驱动的决策)非常有用。在本文中,我们将探讨如何使用 Python 从谷歌地图抓取数据。

步骤 1:安装所需的库

首先,我们需要安装必要的 Python 库,以促进网络抓取过程。打开命令提示符或终端并运行以下命令:

pip install request
pip install beautifulsoup4

requests 库将帮助我们发送 HTTP 请求,而 beautifulsoup4 将帮助解析网页的 HTML 内容。

第 2 步:查找目标 URL

要从 Google 地图抓取数据,我们需要确定包含所需数据的特定 URL。例如,假设我们想要提取有关特定区域餐馆的信息。我们可以在 Google 地图上执行搜索,然后从地址栏复制 URL。URL 通常如下所示:

https://www.google.com/maps/search/restaurants+in+<location>

将 <location> 替换为您要搜索餐馆的城市或地区。

步骤 3:发送 HTTP 请求并检索 HTML 内容

使用请求库,我们可以向目标 URL 发送 HTTP GET 请求并检索页面的 HTML 内容。

import 请求

url = "https://www.google.com/maps/search/restaurants+in+<location>"
response = request.get(url)
html_content = response.text

步骤 4:使用 BeautifulSoup 解析 HTML 内容

获取 HTML 内容后,我们可以使用 beautifulsoup4 库对其进行解析并提取所需的数据。 BeautifulSoup 提供了一种便捷的方式来浏览和搜索 HTML 元素。

示例

在下面的示例中,find_all() 方法用于定位所有带有"section-result-details-container"类的 <div> 元素。在每个餐厅的容器中,我们可以通过使用 find() 浏览 HTML 结构来找到名称和地址。

import requests
from bs4 import BeautifulSoup

url = "https://www.google.com/maps/search/restaurants+in+<location>"
response = requests.get(url)
html_content = response.text

soup = BeautifulSoup(html_content, "html.parser")

restaurants = soup.find_all("div", class_="section-result-details-container")

for restaurant in restaurants:
    name = restaurant.find("h3", class_="section-result-title").text.strip()
    address = restaurant.find("span", class_="section-result-location").text.strip()
    
    print("Name:", name)
    print("Address:", address)
    print("-" * 50)

输出

Café café varanasi
Assi crossing
--------------------------------------------------

Continental Cafe & Cuisine
C- 21/3 1st Floor, Continental Cafe (HHI Campus
--------------------------------------------------

Terracotta Cafe
B1/146 Assi -Pushkar Talab Road Assi Road, Pushkar Talab Rd
--------------------------------------------------

Little Cafe
J7/2-K-7-1
--------------------------------------------------
…

第 5 步:提取其他信息

除了名称和地址,Google 地图还提供许多其他信息,例如评分、评论、营业时间等。我们可以通过探索 HTML 结构并相应地调整代码来提取这些详细信息。

例如,要提取每家餐厅的评分,我们可以按如下方式修改前面的代码:

示例

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_content, "html.parser")

restaurants = soup.find_all("div", class_="section-result-details-container")

for restaurant in restaurants:
    # Code for name and address extraction

    rating_element = restaurant.find("span", class_="cards-rating-score")
    rating = rating_element.text.strip() if rating_element else "N/A"
    
    print("Name:", name)
    print("Address:", address)
    print("Rating:", rating)
    print("-" * 50)

输出

Café café varanasi
Assi crossing
4.6
--------------------------------------------------
Continental Cafe & Cuisine
C- 21/3 1st Floor, Continental Cafe (HHI Campus
4.9
--------------------------------------------------
Terracotta Cafe
B1/146 Assi -Pushkar Talab Road Assi Road, Pushkar Talab Rd
4.5
--------------------------------------------------
Little Cafe
J7/2-K-7-1
4.6
--------------------------------------------------
…

第 6 步:处理分页

在抓取大量数据时,通常会遇到多页搜索结果。Google Maps 利用分页在这些页面之间导航。要从多个页面抓取数据,我们需要识别并跟踪后续页面的 URL。

我们使用其类定位下一页按钮,并从父元素中提取 URL。然后,我们重复发送 HTTP 请求、解析 HTML 和提取每个后续​​页面的数据的过程,直到没有更多可用页面。

结论

在本文中,我们介绍了从 Google Maps 抓取数据的分步过程,包括发送 HTTP 请求、使用 BeautifulSoup 解析 HTML 内容、提取信息和处理分页。此外,请注意,Google Maps 网页的结构可能会随着时间的推移而发生变化,需要调整抓取代码。定期更新和调整您的代码将有助于确保其持续的功能。


相关文章


有用资源