Skip to main content
 首页 » 编程设计

python之Google Places API 返回不正确\不完整的地点

2024年11月24日7jillzhang

我正在设置一个简单的 google places API python 请求代码。 它应该返回所有匹配文本查询的地方。然而,它正在返回其他位置,而不是我正在寻找的位置。

它主要适用于学校、图书馆、教堂、礼拜场所等场所。但是,至少我的一个搜索词没有返回预期的结果,而是返回了几乎所有其他的结果。

import requests 
import json 
import pandas as pd 
from tqdm import tqdm 
 
global points 
points = pd.DataFrame() 
search = pd.read_csv("Search Terms.csv") 
global page 
global Type 
page = 1 
APIKEY = "MY API KEY" 
 
def findPlaces(loc=("-33.8901","150.7191000"),radius=35000, pagetoken = None): 
   global points 
   global page 
   global Type 
   global Term 
   lat, lng = loc 
   type = Term 
   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 "") 
   response = requests.get(url) 
   res = json.loads(response.text) 
   print(Type, "page: ", page) 
   for result in res["results"]: 
 
 
      temp = { 
          "Name" : result["name"], 
          "Lat" : result["geometry"]["location"]["lat"], 
          "Lon" : result["geometry"]["location"]["lng"], 
          "Type" : Type, 
          "Search Term" : Term 
          } 
      points = points.append(temp, ignore_index= True) 
   pagetoken = res.get("next_page_token",None) 
 
   page += 1 
   return pagetoken 
 
pagetoken = None 
 
 
for index, row in search.iterrows(): 
    termraw = row['Terms'] 
    Type = row['Type'] 
    terms = termraw.split(',') 
    i = 0 
    while i < len(terms): 
        Term = terms[i] 
        pagetoken = findPlaces(pagetoken=pagetoken) 
        import time 
        time.sleep(5) 
 
        if not pagetoken: 
            i += 1 
 
print("cleaning") 
points = points.drop_duplicates(subset ="Name", keep = 'first')  
 
points.to_csv('outputaged.csv') 
 

就我的理解而言,如果我输入例如“疗养院”,我应该得到类似于 https://www.google.com/maps/search/Nursing+home/@-33.8132784,150.9377676,12z 的返回

然而,实际的结果似乎是排除了这些东西的一切。

我从来没有真正做过任何谷歌 API 请求,所以任何帮助都会非常有用。 谢谢,

附上输出的片段。

0   -33.8688197 151.2092955 Sydney 
1   -33.9166741 150.8525853 Bunnings Hoxton Park 
2   -33.8641885 150.6407872 BP 
3   -33.7961736 150.7070948 Penrith Anglican College 
4   -33.8747193 150.7150313 Workers Hubertus Country Club 
5   -33.7849683 150.6752473 Caroline Chisholm College 
6   -33.7877012 150.7714889 Blue Cattle Dog Hotel 
7   -33.7843743 150.7087178 Penrith Christian School 
8   -33.789197  150.668553  Subway® Restaurant 
9   -33.925649  150.854178  Goodstart Early Learning Hoxton Park 
11  -33.7889187 150.6689905 Asian Pearl Chinese Restaurant 
12  -33.8367893 150.7617273 Twin Creeks Golf & Country Club 
15  -33.9090231 150.7862319 Czecho Slovakian Country Club 
16  -33.8956637 150.8520624 Cecil Hills High School 
17  -33.803243  150.804976  Liquorland Erskine Park 
18  -33.7942519 150.788228  Red Rooster 
19  -34.065         150.814167  Campbelltown 
20  -33.9372737 150.845665  Carnes Hill Marketplace 
21  -33.7888697 150.6684124 Glenmore Park Realty 
 

请您参考如下方法:

对于以后遇到此问题的任何人,问题如下: 我为 API 提供了一个“类型”键而不是“关键字”键。这没有很好的记录。

类型必须在 this 中列表。否则它将返回该位置的所有内容