【问题标题】:Python, Google Places APIPython,谷歌地方API
【发布时间】:2018-05-28 21:38:05
【问题描述】:

我正在尝试使用对 Google Places API 的 python 请求在给定半径内的某个位置获取所有类型的餐厅。这是我的简单代码:

import requests
import json

APIKEY = "AIzaSyBFx8hqftDOlrSWRTiOSowjwfeS1OQtBpw"

def findPlaces(loc=("51.1079","17.0385"),radius=3000, pagetoken = None):
   lat, lng = loc
   type = "restaurant"
   url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius={radius}&type={type}&key={APIKEY}{pagetoken}".format(lat = lat, lng = lng, radius = radius, type = type,APIKEY = APIKEY, pagetoken = "&pagetoken="+pagetoken if pagetoken else "")
   print(url)
   response = requests.get(url)
   res = json.loads(response.text)
   print(res)
   for result in res["results"]:
      info = ";".join(map(str,[result["name"],result["geometry"]["location"]["lat"],result["geometry"]["location"]["lng"],result.get("rating",0),result["place_id"]]))
      print(info)
   pagetoken = res.get("next_page_token",None)
   return pagetoken


pagetoken = None
while True:
     pagetoken = findPlaces(pagetoken=pagetoken)
     if not pagetoken:
         break

但是,即使它在结果的第一页上运行良好,但在没有明显原因的情况下无法进入第二页(它只是从 API 返回 INVALID REQUEST)。此外,有趣的是,由于我将我的 url 打印到控制台,我只需单击它就可以跟踪这个非常具体的 url,当我这样做时,它运行良好,API 返回所需的餐馆列表。

我认为这是 url 编码字符串的问题,我用 python 的 urlencode 替换了格式,但是 - 结果保持不变。由于我的想法已经用完了,我想问:有人与这样的问题有关吗?

提前感谢您的帮助。

【问题讨论】:

    标签: python request google-places-api


    【解决方案1】:

    我用新的纬度和经度对其进行了测试,得到了 3 页,每 20 页,如 google 文档所说,总计 60 页。

    https://developers.google.com/places/web-service/search

    next_page_token 包含一个可用于返回最多 20 个的令牌 额外的结果。如果存在,则不会返回 next_page_token 没有要显示的其他结果。最大结果数 可以返回的是 60。 next_page_token 的颁发,何时生效。

    import requests
    import json
    
    APIKEY = "AIzaSyBFx8hqftDOlrSWRTiOSowjwfeS1OQtBpw"
    
    def findPlaces(loc=("35.701474","51.405288"),radius=4000, pagetoken = None):
       lat, lng = loc
       type = "restaurant"
       url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lng}&radius={radius}&type={type}&key={APIKEY}{pagetoken}".format(lat = lat, lng = lng, radius = radius, type = type,APIKEY = APIKEY, pagetoken = "&pagetoken="+pagetoken if pagetoken else "")
       print(url)
       response = requests.get(url)
       res = json.loads(response.text)
       # print(res)
       print("here results ---->>> ", len(res["results"]))
    
       for result in res["results"]:
          info = ";".join(map(str,[result["name"],result["geometry"]["location"]["lat"],result["geometry"]["location"]["lng"],result.get("rating",0),result["place_id"]]))
          print(info)
       pagetoken = res.get("next_page_token",None)
    
       print("here -->> ", pagetoken)
    
       return pagetoken
    
    # pagetoken = "CpQFhwIAADQWOcVI1wll-B869Z24El48rXw18gKoab_keD65V18zFEvPjKIfrS79Pc_vXJcZQtOuF0RObQG20ph-GE3ssP3k1fu8zsYbw5g3UPbSjAvQLdXkdD1qAWztXj7hc5Kxc4pYRyGM1_ljVOHg3Py_zSlYscnoNjCvRua2MDQgusCsEquNqGREFdvhjDkbeMhEFYxHucTnIn96OxIJEpamePTHsBooYyPBaa_ejGZ_C99QeDjpSkSKBgEe3aL1uWKlYhsGKh7biQUR5rKsKPodwccLIrW8Gr5tag3NH0sLPExHHvqzlpkj--KIuydTVjPH7u2zHxmPByServ2S5xjXYUBRr-ly3e1xPsVMhZZH9TxfttCIHLscBvpvCswIfaGYdl3bEzsrFISfpp0rpKtlp9gWGY7Tbk2n6s3etCHQEHn2qmM8bsJwkZV81pUWN0j9C9RX-ywOyIKY2yp1w_Iq1mRwOwY4mckbicOoooHiV6JER4xe7Kizw9hbXOnezn_NMk15TLwRoXlfL1s73uwogo-VWE8c-V1HqRpWQSyudRhLwhOEclrICXIdxICOgTgYO1z57xCEerw3QUL_7MPDrlbbh_AlX8I6Jfe8IhQ1Fkqu_njatm6aBTjkp2CSqlvZJpI_Lrv330VcyFEqBkGn7NJew3I9xofSrBaXFa8ABi6DXQm6-yC32OEyf7GHNXINjT1IB0yh6KR6c0qzaqiqOzKcuuai9XqEMQNNKyi6EuhzH5TP9YA56N3JhnXRFhs2aWHZhLlieVI6_uqzpZSgYjUem8aQrMTlmHw0kIYU8I-Ca041C4Zm2gMezwygRrhzsOoAmbmu96nft0KuIWTB3A_xGVKYQ2qjb2KRM7nsglnSEhDoNs8EhvuIm0FQs30YSCp5GhRO3b3Tn5rsLuwiWgu8hwEGhL0S1A"
    pagetoken = None
    
    while True:
         pagetoken = findPlaces(pagetoken=pagetoken)
         import time
         time.sleep(5)
    
         if not pagetoken:
             break
    

    【讨论】:

    • 非常感谢!我承认我个人永远不会认为有延迟并且需要以这种方式处理它。非常感谢您所做的努力,再次感谢!
    • 您是否也获得了 3 页? @MichałKaczanowicz
    • 是的,我做了,3 页,60 个结果
    • 我使用了完全相同的代码,但在 next_page_token 上出现了 keyerror
    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多