如何使用Python抓取谷歌地圖資料?
谷歌地圖是一個強大的工具,它提供了大量的地理空間資料,包括位置、地址、評論、評分等等。能夠以程式設計方式提取這些資料對於各種應用都非常有用,例如商業分析、研究和資料驅動的決策。在本文中,我們將探討如何使用Python抓取谷歌地圖資料。
步驟1:安裝所需的庫
首先,我們需要安裝必要的Python庫來促進網頁抓取過程。開啟你的命令提示符或終端並執行以下命令:
pip install requests pip install beautifulsoup4
requests庫將幫助我們傳送HTTP請求,而beautifulsoup4將幫助解析網頁的HTML內容。
步驟2:查詢目標URL
要從谷歌地圖抓取資料,我們需要確定包含所需資料的特定URL。例如,假設我們想提取某個區域餐廳的資訊。我們可以在谷歌地圖上進行搜尋,然後從位址列複製URL。URL通常如下所示:
https://www.google.com/maps/search/restaurants+in+<location>
將<location>替換為你想要搜尋餐廳的城市或區域。
步驟3:傳送HTTP請求並檢索HTML內容
使用requests庫,我們可以向目標URL傳送HTTP GET請求並檢索頁面的HTML內容。
import requests url = "https://www.google.com/maps/search/restaurants+in+<location>" response = requests.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:提取附加資訊
除了名稱和地址外,谷歌地圖還提供許多其他資訊,例如評分、評論、營業時間等等。我們可以透過探索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:處理分頁
在抓取大量資料時,通常會遇到多個搜尋結果頁面。谷歌地圖使用分頁在這些頁面之間導航。要從多個頁面抓取資料,我們需要識別並跟蹤後續頁面的URL。
我們使用其類定位下一頁按鈕,並從父元素中提取URL。然後,我們重複傳送HTTP請求、解析HTML和提取每個後續頁面的資料,直到沒有更多頁面可用。
結論
在本文中,我們介紹了從谷歌地圖抓取資料的逐步過程,包括髮送HTTP請求、使用BeautifulSoup解析HTML內容、提取資訊和處理分頁。此外,請注意,谷歌地圖網頁的結構可能會隨著時間的推移而發生變化,這需要調整抓取程式碼。定期更新和調整你的程式碼將有助於確保其持續的功能。