你可以使用google-search-results包来抓取谷歌地图。
Full example at Repl.it.
import os
from serpapi import GoogleSearch
params = {
"engine": "google_maps",
"q": "coffee",
"type": "search",
"ll": "@40.7455096,-74.0083012,14z",
"api_key": os.getenv("API_KEY")
}
client = GoogleSearch(params)
data = client.get_dict()
print("Local results")
for result in data['local_results']:
print(f"""
Title: {result['title']}
Address: {result['address']}
Rating: {result['rating']}
Reviews: {result['reviews']}""")
if 'ads_results' in data:
print("Ads")
for result in data['ads_results']:
print(f"""
Title: {result['title']}
Address: {result['address']}""")
JSON 响应
{
"local_results": [
{
"position": 1,
"title": "Birch Coffee",
"data_id": "0x89c258ef40975c2b:0x4fa24ff965c3f3e",
"gps_coordinates": {
"latitude": 40.7638094,
"longitude": -73.9666075
},
"rating": 4.5,
"reviews": 477,
"price": "$$",
"type": "Coffee shop",
"address": "134 1/2 E 62nd St, New York, NY 10065",
"hours": "Open until 7:00 PM",
"phone": "(212) 686-1444",
"website": "http://www.birchcoffee.com/",
"description": "Hip spot offering house-roasted brews. Local coffeehouse chain serving thoughtfully-sourced, house-roasted brews in a hip, bustling space.",
"thumbnail": "https://lh5.googleusercontent.com/p/AF1QipPy035-T0IVHuC3CffD8UEf0n70HkkZXvkb7gSJ=w122-h92-k-no"
},
{
"position": 2,
"title": "Think Coffee",
"data_id": "0x89c259ca0a28731f:0xd3d13e0daf7fae6c",
"gps_coordinates": {
"latitude": 40.7522222,
"longitude": -74.0016667
},
"rating": 3.9,
"reviews": 467,
"price": "$$",
"type": "Coffee shop",
"address": "500 W 30th St, New York, NY 10001",
"website": "http://www.thinkcoffee.com/",
"thumbnail": "https://lh5.googleusercontent.com/p/AF1QipMIVRZJMr-bnGKw28VTrctmhVYQOnIKBRj0NmnN=w122-h92-k-no"
}
// Stripped...
]
}
输出
Local results
Title: Think Coffee
Address: 73 8th Ave, New York, NY 10014
Rating: 4.2
Reviews: 741
Title: Birch Coffee @Flatiron
Address: 21 E 27th St, New York, NY 10016
Rating: 4.4
Reviews: 940
Title: Irving Farm New York
Address: 135 E 50th St, New York, NY 10022
Rating: 4.3
Reviews: 248
// Stripped...
Ads
Title: Gotham Coffee Roasters
Address: 23 W 19th St, New York, NY 10011
披露:我在 SerpApi 工作。