我想开发一个地图应用程序,它可以显示给定地点附近的银行。
我使用Places库进行搜索,但每次只返回20个结果。如果我想要更多的结果,我该怎么做?
我想开发一个地图应用程序,它可以显示给定地点附近的银行。
我使用Places库进行搜索,但每次只返回20个结果。如果我想要更多的结果,我该怎么做?
更新: 自从我最初写下这篇答案以来,API已经进行了增强,使得这个答案已经过时(或者至少是不完整的)。请参见如何从Google Places API获取20个以上的结果?以获取更多信息。
原始答案:
文档显示Places API返回最多20个结果。它没有表明有任何改变那个限制的方法。所以,简短的答案似乎是:你不能。
当然,你可能能够通过查询几个位置来模拟它,并合并/去重结果。虽然这是一种廉价的hack,但可能效果不佳。在此之前,我会先检查一下它是否违反了服务条款。
现在API支持更多的结果(最多60个),使用一个参数page_token可以获取前面搜索结果的下一批20个结果。
使用page_token参数可以获取之前搜索相同条件的下一批20个结果,其他所有参数除了page_token都会被忽略。
你还可以参考访问额外的结果部分来查看如何进行分页的示例。
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var url = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={args[0]}&radius={args[1]}&type=restaurant&keyword={args[2]}&key={args[3]}";
dynamic res = null;
var places = new List<PlacesAPIRestaurants>();
using (var client = new HttpClient())
{
while (res == null || HasProperty(res, "next_page_token"))
{
if (res != null && HasProperty(res, "next_page_token"))
{
if (url.Contains("pagetoken"))
url = url.Split(new string[] { "&pagetoken=" }, StringSplitOptions.None)[0];
url += "&pagetoken=" + res["next_page_token"];
}
var response = client.GetStringAsync(url).Result;
JavaScriptSerializer json = new JavaScriptSerializer();
res = json.Deserialize<dynamic>(response);
if (res["status"] == "OK")
{
foreach (var place in res["results"])
{
var name = place["name"];
var rating = HasProperty(place,"rating") ? place["rating"] : null;
var address = place["vicinity"];
places.Add(new PlacesAPIRestaurants
{
Address = address,
Name = name,
Rating = rating
});
}
}
else if (res["status"] == "OVER_QUERY_LIMIT")
{
return;
}
}
}
}
public static bool HasProperty(dynamic obj, string name)
{
try
{
var value = obj[name];
return true;
}
catch (KeyNotFoundException)
{
return false;
}
}
}
}
您可以爬取Google Places的结果并跟随分页,以获取特定位置的200-300个地点(从10到15页的搜索结果)。
或者,您可以使用SerpApi访问从Google Places提取的数据。它有一个免费试用版。
# Package: https://pypi.org/project/google-search-results
from serpapi import GoogleSearch
import os
params = {
"api_key": os.getenv("API_KEY"),
"engine": "google",
"q": "restaurants",
"location": "United States",
"tbm": "lcl",
"start": 0
}
search = GoogleSearch(params)
data = search.get_dict()
for local_result in data['local_results']:
print(
f"Position: {local_result['position']}\nTitle: {local_result['title']}\n"
)
while ('next' in data['serpapi_pagination']):
search.params_dict["start"] += len(data['local_results'])
data = search.get_dict()
print(f"Current page: {data['serpapi_pagination']['current']}\n")
for local_result in data['local_results']:
print(
f"Position: {local_result['position']}\nTitle: {local_result['title']}\n"
)
输出
Current page: 11
Position: 1
Title: Carbone
Position: 2
Title: Elmer's Restaurant (Palm Springs, CA)
Position: 3
Title: The Table Vegetarian Restaurant
...
免责声明:我在SerpApi工作。
不确定您是否可以获得更多信息。
Places API 最多返回 20 个机构结果。
http://code.google.com/apis/maps/documentation/places/#PlaceSearchResponses