Google Maps 抓取工具
目录
免费 Google Maps 抓取工具
使用这款免费爬虫工具从 Google Maps 提取商家评论。只需几个步骤,即可获取评论者、评分、评论文本、照片等详细信息。
设置和安装
开始之前,请确保已安装以下必备项:
- Python 3.9+
- Playwright(用于浏览器自动化)
按照以下步骤完成所有设置:
-
将代码仓库克隆到本地计算机
-
进入 free-scraper 目录
-
安装所需依赖项:
pip install playwright
playwright install
如何使用爬虫工具
下面介绍如何使用该抓取工具:
-
打开 main.py 并添加目标 Google Maps URL:
urls = [
"https://www.google.com/maps/place/Joe's+Pizza+Broadway/@40.7546835,-73.989604,17z/data=!3m1!5s0x89c259ab3e91ed73:0x4074c4cfa25e210b!4m8!3m7!1s0x89c259ab3c1ef289:0x3b67a41175949f55!8m2!3d40.7546795!4d-73.9870291!9m1!1b1!16s%2Fg%2F11bw4ws2mt?entry=ttu",
"https://www.google.com/maps/place/Googleplex/@37.4220583,-122.0878991,17z/data=!4m8!3m7!1s0x808fba02425dad8f:0x6c296c66619367e0!8m2!3d37.4220541!4d-122.0853242!9m1!1b1!16zL20vMDNiYnkx?hl=en&entry=ttu",
# Add as many URLs as you need
]
-
使用以下命令运行脚本:
python main.py
输出
爬虫工具会将结果保存到 free_scraper_output.json,其中包含:
{
"reviewer_name": "Jacqueline",
"reviewer_link": "https://www.google.com/maps/contrib/108281841745817069467/reviews?hl=en-GB",
"reviewer_image": "https://lh3.googleusercontent.com/a-/ALV-UjUzl62rTNZOxsKGxvlnjM3leUg7DZostYzDJvG_8DUSNEtC7p-X=w36-h36-p-rp-mo-ba4-br100",
"rating": 5,
"date": "a week ago",
"text": "Went for lunch on a Tuesday at around 12:45pm. As usual, there was a line, but it moved really quickly and I was able to get my pizza 10 minutes later. Don't let the line deter you from trying Joe's! The pizza is great as always and the staff are nice. Never disappointed. I tried the caprese pizza and it was great!\n\nThere are a lot of people inside and outside so it can get hectic, but turnover for seats is quite quick and I got a seat on the bench outside almost right away.",
"photos": [
"https://lh5.googleusercontent.com/p/AF1QipMChGwUdQgWF9NAqdvss36KeuBZw_DAiuZy5yJj=w300-h225-p-k-no",
"https://lh5.googleusercontent.com/p/AF1QipNvachPq2HyJU0_3hs0DsRDRChaHZrQDDKOc3hz=w300-h225-p-k-no"
],
"likes_count": "1"
}
每条评论记录包含:
- 评论者详细信息:姓名、个人资料链接和头像图片
- 评论内容:星级评分、发布日期和完整评论文本
- 媒体:所有附加照片的 URL
- 互动情况:评论获得的点赞数
常见抓取挑战
从 Google Maps 抓取数据可能非常困难。以下是你可能遇到的一些常见问题:
- 动态内容加载: Google Maps 采用动态加载机制,当用户滚动页面时,通过 XHR/API 调用获取评论。如果未正确处理这些动态请求并等待内容加载,爬虫可能只能抓取到部分数据。
- DOM 结构变更: Google 经常更新其 DOM 结构、类名和数据属性。因此,需要定期维护抓取逻辑,以适应结构变化。
- 速率限制和检测: 抓取 Google Maps 可能会触发反机器人防御机制,从而导致速率限制或 IP 被封禁。
解决方案:Bright Data Google Maps 爬虫工具 API
如果你需要可靠的大规模数据提取,请使用 Bright Data Google Maps 爬虫工具 API。它更具优势,原因如下:
- 无需管理代理
- 可从全球任意地点抓取
- 可使用覆盖 195 个国家/地区的 7200 多万个真实 IP
- 支持多种数据交付方式(S3、Cloud Storage 等)
- 符合 GDPR 和 CCPA 要求
- 全天候技术支持
此外,你还可以获得 20 次免费 API 调用进行测试。
使用 Google Maps 爬虫工具 API
只需提供 URL,即可采集详细的 Google Maps 评论。
有关设置 Google Maps 爬虫工具 API 的详细说明,请查看我们的分步设置指南。
主要输入参数:
| 参数 |
类型 |
说明 |
必填 |
url |
字符串 |
Google Maps 商家 URL |
是 |
days_limit |
数字 |
要检索多少天内的评论 |
否 |
输出数据示例:
{
"url": "https://www.google.com/maps/place/Apple+Apple+Park+Visitor+Center/@37.3327772,-122.0079593,17z/data=!4m18!1m9!3m8!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!2sApple+Apple+Park+Visitor+Center!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg!3m7!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg?entry=ttu",
"place_id": "ChIJ0aPn18W1j4ARgC9zTSPeQRc",
"place_name": "Apple Apple Park Visitor Center",
"country": "US",
"address": "10600 N Tantau Ave, Cupertino, CA 95014",
"review_id": "ChdDSUhNMG9nS0VJQ0FnSUMzcDdqMHJRRRAB",
"reviewer_name": "Krzysztof Wojtczak",
"reviews_by_reviewer": 32,
"photos_by_reviewer": "12",
"reviewer_url": "https://www.google.com/maps/contrib/110797405132582450368/reviews?hl=en",
"local_guide": true,
"review_rating": 5,
"review": "Fajnie można spędzić czas, czilując na tarasie z kawką 🤙",
"review_date": "2024-11-09T22:18:01.713Z",
"number_of_likes": 0,
"response_of_owner": null,
"response_date": null,
"photos": [
"https://lh5.googleusercontent.com/p/AF1QipOtSDFfVpLL5GopXJzoFbTapha0G91nmEx5aSus",
"https://lh5.googleusercontent.com/p/AF1QipPcr5_TU6MWhAu1_K54DbxywZ4O88Q_qT7NSnhv",
"https://lh5.googleusercontent.com/p/AF1QipNLM5jHD0iDozGHsax9ydGAWUI_pn_fujQ3Q4Wr",
"https://lh5.googleusercontent.com/p/AF1QipPcKPwXyHDawuClpTpfV3zE7vMFO0UabmdjX9IG"
],
"timestamp": "2024-11-10T07:37:58.326Z",
"input": {
"url": "https://www.google.com/maps/place/Apple+Apple+Park+Visitor+Center/@37.3327772,-122.0079593,17z/data=!4m18!1m9!3m8!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!2sApple+Apple+Park+Visitor+Center!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg!3m7!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg?entry=ttu",
"days_limit": 30
}
}
下载此示例 JSON 文件即可查看完整输出。
代码示例:
以下 Python 脚本用于采集 Google Maps 评论,并将结果存储到 JSON 文件中:
import requests
import json
import time
class BrightData:
def __init__(self, api_token):
self.api_token = api_token
self.headers = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json",
}
def collect_reviews(self, urls_data):
"""
Collect Google Maps reviews using BrightData API
"""
# 1. Trigger data collection
print("Starting data collection...")
trigger_response = self._trigger_collection(urls_data)
snapshot_id = trigger_response.get("snapshot_id")
print(f"Snapshot ID: {snapshot_id}")
# 2. Wait for data to be ready
print("Waiting for data...")
while True:
status = self._check_status(snapshot_id)
print(f"Status: {status}")
if status == "ready":
# Check if data is actually available
data = self._get_data(snapshot_id)
if data and len(data) > 0:
break
time.sleep(10) # Wait 10 seconds before next check
# 3. Get and save the data
print("Saving data...")
filename = f"api_scraper_output.json"
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"✓ Data saved to {filename}")
print(f"✓ Collected {len(data)} reviews")
return data
def _trigger_collection(self, urls_data):
"""Trigger data collection"""
response = requests.post(
"https://api.brightdata.com/datasets/v3/trigger",
headers=self.headers,
params={"dataset_id": "gd_luzfs1dn2oa0teb81",
"include_errors": "true"},
json=urls_data,
)
return response.json()
def _check_status(self, snapshot_id):
"""Check collection status"""
response = requests.get(
f"https://api.brightdata.com/datasets/v3/progress/{snapshot_id}",
headers=self.headers,
)
return response.json().get("status")
def _get_data(self, snapshot_id):
"""Get collected data"""
response = requests.get(
f"https://api.brightdata.com/datasets/v3/snapshot/{snapshot_id}",
headers=self.headers,
params={"format": "json"},
)
return response.json()
if __name__ == "__main__":
# Initialize with your API token
brightdata = BrightData("<YOUR_API_TOKEN>")
# Define URLs to collect reviews from
urls_to_collect = [
{
"url": "https://www.google.com/maps/place/Apple+Apple+Park+Visitor+Center/@37.3327772,-122.0079593,17z/data=!4m18!1m9!3m8!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!2sApple+Apple+Park+Visitor+Center!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg!3m7!1s0x808fb5c5d7e7a3d1:0x1741de234d732f80!8m2!3d37.332773!4d-122.0053844!9m1!1b1!16s%2Fg%2F11rjtz4vtg?entry=ttu",
"days_limit": 30,
},
{
"url": "https://www.google.com/maps/place/Joe's+Pizza+Broadway/@40.7546835,-73.989604,17z/data=!3m1!5s0x89c259ab3e91ed73:0x4074c4cfa25e210b!4m8!3m7!1s0x89c259ab3c1ef289:0x3b67a41175949f55!8m2!3d40.7546795!4d-73.9870291!9m1!1b1!16s%2Fg%2F11bw4ws2mt?entry=ttu",
"days_limit": 25,
},
]
# Collect reviews
reviews = brightdata.collect_reviews(urls_to_collect)
代码工作原理:
- 需要 API Token: 首先,你需要一个 API Token。如果还没有,请按照我们的 Google Maps 爬虫工具 API 设置指南进行操作。
- 开始数据采集: 将你的 API Token 传递给代码,代码将使用你指定的参数启动数据采集。系统会返回一个
snapshot_id,供你跟踪请求。
- 等待结果: 数据采集需要几分钟才能完成。在此期间,我们的代码会持续检查
snapshot_id 状态:
- 状态为“running”= 仍在采集数据
- 状态为“ready”= 数据采集已完成并保存到你的 JSON 文件中
- 其他参数: 你可以通过向
_trigger_collection 函数添加更多参数来自定义数据采集。请查看下一部分,了解可用参数及不同的数据交付方式。
使用 API 参数自定义数据采集
使用以下 API 参数自定义数据采集:
| 参数 |
类型 |
说明 |
示例 |
limit |
integer |
限制每个输入返回的结果数量。 |
limit=10 |
include_errors |
boolean |
在输出中包含错误报告,以便进行问题排查。 |
include_errors=true |
notify |
url |
采集完成后发送通知的 URL。 |
notify=https://notify-me.com/ |
format |
enum |
数据交付格式。支持的格式:JSON、NDJSON、JSONL、CSV。 |
format=json |
💡其他交付方式: 你可以选择通过 webhook 或 API 交付数据。
评论