Google在Python中放置API

2024-09-30 20:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python中的googleplacesapi来查找城市/城镇/地方(给定为位置)中的任何内容(以查询形式给出)。以下是我的代码片段:

from googleplaces import GooglePlaces, types, lang


YOUR_API_KEY = 'xxxx'
google_places = GooglePlaces(YOUR_API_KEY)

query_result = google_places.text_search(
        query='Play Schools', location = 'Mumbai, India')
print(len(query_result.places))
for place in query_result.places:
    print (place.name)
    print (place.place_id)
    print('\n')

if (query_result.has_next_page_token):
query_result_next_page = google_places.text_search(
        pagetoken=query_result.next_page_token)

现在的问题是,我的代码将结果的数量限制为20个,并且googlemaps在同一个查询中返回了20多个结果。如何检索所有结果?在

编辑:我添加了最后3行代码,试图在下一页上获得结果,但是我被抛出了无效的请求错误。在

谢谢你读我的问题。在


Tags: key代码textapisearchyourgooglepage
2条回答

我发现出了什么问题。系统抛出无效的\u请求,因为next_page_令牌无效(在发出下一个\u page_令牌到它将生效之间有一个小的延迟b/w)。我编译了代码的前半部分(不包括最后三行),然后编译了最后三行代码,它成功了!!所以据我所知,延误是造成错误的原因。在

所以现在我要做的就是在最后一个if条件之前设置1-2秒的计时器。在

感谢所有对这个问题发表评论和回答的人。你们是这个社区的英雄。在

By default, each Nearby Search or Text Search returns up to 20 establishment results per query; however, each search can return as many as 60 results, split across three pages. If your search will return more than 20, then the search response will include an additional value — next_page_token. Pass the value of the next_page_token to the pagetoken parameter of a new search to see the next set of results. If the next_page_token is null, or is not returned, then there are no further results. There is a short delay between when a next_page_token is issued, and when it will become valid. Requesting the next page before it is available will return an INVALID_REQUEST response. Retrying the request with the same next_page_token will return the next page of results.

请检查此url:HERE

将PagerToken作为附加参数添加到HTTP请求url中,如下所示

https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken=CpQCAgEAAFxg8o-eU7_uKn7Yqjana-HQIx1hr5BrT4zBaEko29ANsXtp9mrqN0yrKWhf-y2PUpHRLQb1GT-mtxNcXou8TwkXhi1Jbk-ReY7oulyuvKSQrw1lgJElggGlo0d6indiH1U-tDwquw4tU_UXoQ_sj8OBo8XBUuWjuuFShqmLMP-0W59Vr6CaXdLrF8M3wFR4dUUhSf5UC4QCLaOMVP92lyh0OdtF_m_9Dt7lz-Wniod9zDrHeDsz_by570K3jL1VuDKTl_U1cJ0mzz_zDHGfOUf7VU1kVIs1WnM9SGvnm8YZURLTtMLMWx8-doGUE56Af_VfKjGDYW361OOIj9GmkyCFtaoCmTMIr5kgyeUSnB-IEhDlzujVrV6O9Mt7N4DagR6RGhT3g1viYLS4kO5YindU6dm3GIof1Q&key=YOUR_API_KEY

复制粘贴这个网址在浏览器上仔细检查。另外,请仔细阅读文档url。干杯!在

相关问题 更多 >