使用python抓取谷歌地图,如何获得更多结果?



我的目标是获得一个全面的棋盘游戏咖啡馆列表,以获得这类事情的数据:https://localtrust.org.uk/insights/essays/skittled-out-an-essay-by-dan-gregory/

我的代码目前工作良好,除了只得到20个结果,尽管"num": "300"我使用的是免费版的serpapi,所以如果可能的话,我不想使用所有100个搜索来测试想法。我会看看能否得到资金升级。

在Anaconda中使用JupyterLab下面是我的代码:

# Sidenote: Redundant imports(?) Didn't work when I only imported serpapi as sp, but did with the line "from..." - would like to know why
import serpapi as sp
import numpy as np
import pandas as pd
import json
import requests
from serpapi import GoogleSearch
import csv
# Set up the query parameters
params = {
"engine": "google_maps",
"q": "board game cafe",
"google_domain": "google.co.uk",
"gl": "uk",
"num": "300",
"api_key": "7ced5dc1bb79652a66f31ce95172d460bf86be8371bb354165d1a3edfd804261"
}
# Send the request and get the JSON response
search = GoogleSearch(params)
results = search.get_dict()
end_results = results["local_results"]
# Extract the relevant information from the JSON response
cafes = ["name", "longitude", "latitude"]
for result in end_results:
name = result["title"]
latitude = result['gps_coordinates']['latitude']
longitude = result['gps_coordinates']['longitude']
cafes.append({'name': name, 'latitude': latitude, 'longitude': longitude})
# convert the dataframe to a CSV file
with open('cafes.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Latitude', 'Longitude'])  # write header row
for result in local_results:
name = result["title"]
lat = result['gps_coordinates']['latitude']
lon = result['gps_coordinates']['longitude']
writer.writerow([name, lat, lon])  # write data row

我已经尝试搜索堆栈溢出,我正在考虑尝试逐镇搜索,例如"q": "board game cafe stockport"使用城镇列表和最大值。半径5英里(如果半径是一个可能的参数)。同样,我不想使用所有100个免费搜索。

不幸的是,在SerpApi的Google Maps API中,没有办法增加单个API调用的结果数量。如果您想提取超过20个结果,则需要使用start(documentation)参数对结果进行分页。

也没有可以用来将搜索范围缩小到特定区域的半径参数。但是,您应该考虑使用GPS坐标作为ll参数(文档)。该参数的一部分是zoom,它接受3z(地图完全缩小)和21z(地图完全放大)之间的值。有了这个,你可以操纵你的结果,从更小的区域获得数据。

如果你觉得100个搜索积分对你的测试来说还不够,你可以随时联系contact@serpapi.com,找到一个解决方案,让你自由地测试你的实现。

顺便说一下,你在帖子中分享了你的API密钥。我建议再生它。否则,其他人可能会使用它。:)