我尝试使用Python和Google Maps API在Google中查找一些酒店的公共电话号码,但我的Python代码无法找到任何电话号码

pod7payv  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(102)

这是我的Python代码:

import requests 
import json

def search_place(api_key, place):
    base_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
    inputtype = "textquery"
    fields = "formatted_address,name,formatted_phone_number"
    locationbias = "ipbias"

    request_url = f"{base_url}input={place}&inputtype={inputtype}&fields={fields}&locationbias={locationbias}&key={api_key}"

    res = requests.get(request_url)
    data = json.loads(res.text)

    return data

api_key = "<my api key>"  

hotel_list = [
    "White castle",
    "Mihilaka holiday homes",
    
]

with open("hotel_numbers.txt", "w") as file:
    for hotel in hotel_list:
        data = search_place(api_key, hotel)
        if 'candidates' in data and data['candidates']:
            hotel_data = data['candidates'][0]
            name = hotel_data.get('name', '')
            phone = hotel_data.get('formatted_phone_number', "Can't find")
            file.write(f"{name}: {phone}\n")
        else:
            file.write(f"{hotel}: Can't find\n")

字符串
有人可以解决我的问题或给予解决方案吗?

bprjcwpo

bprjcwpo1#

如果Find Place没有返回您想要的字段,可以使用Find Place获取一个place_id,然后使用该place_id进行Place Details请求

我试着在我自己的环境中运行你的代码,稍微调整了一下,我可以看到你的函数返回一个INVALID_REQUEST错误,说你输入的字段无效。根据文件

Place Search请求和Place Details请求返回的字段不相同。Place Search请求返回的字段是Place Details请求返回字段的子集。如果Place Search没有返回您想要的字段,您可以使用Place Search获取一个place_id,然后使用该Place ID进行Place Details请求。

这意味着,您可以先使用Find Place来获取place_id,然后使用Place Details来获取您想要的所有细节,而不是使用Find Place来获取您想要的所有细节。有关详细信息,请参阅Place Details documentation。但上面说,
一旦您从地点搜索中获得place_id,您可以通过发起地点详细信息请求来请求有关特定机构或兴趣点的更多详细信息。地点详细信息请求返回有关所指示地点的更全面的信息,例如其完整地址、电话号码、用户评级和评论。
但是,您必须注意使用字段功能来降低一些成本。
因此,你的代码应该看起来像这样:

import requests 
import json

def search_place(api_key, place):

    # Use place search first to get the place_id to be used for Place Details request later.
    place_search_base_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
    inputtype = "textquery"

    # Set the fields for place_id only to cut cost.
    fields = "place_id"
    locationbias = "ipbias"

    request_url = f"{place_search_base_url}input={place}&inputtype={inputtype}&fields={fields}&locationbias={locationbias}&key={api_key}"

    res = requests.get(request_url)
    data = json.loads(res.text)

    return data

api_key = "YOUR_API_KEY"  

hotel_list = [
    "White castle",
    "Mihilaka holiday homes",
]

for hotel in hotel_list:
  data = search_place(api_key, hotel)
  if 'candidates' in data and data['candidates']:

      # Here we try to fetch the place_id
      hotel_place_id = data['candidates'][0]["place_id"]

      # create another base url for Place Detail request
      place_details_base_url = "https://maps.googleapis.com/maps/api/place/details/json?"

      # This should be the place to put the fields you want to cut cost.
      place_details_fields = "name,formatted_address,formatted_phone_number"

      place_details_url = f"{place_details_base_url}place_id={hotel_place_id}&fields={place_details_fields}&key={api_key}"
      place_details_res = requests.get(place_details_url)

      # Convert the response to json
      place_details_data = json.loads(place_details_res.text)

      # Then fetch the name and the phone from the Place Details result
      name = place_details_data["result"].get('name', '')
      phone = place_details_data["result"].get('formatted_phone_number', 'can\'t find')

      # This should print the name and the formatted phone number if it exists.
      print(f"{name}: {phone}\n")
  else:
      print(f"{hotel}: Can't find\n")

字符串
通过这段代码,我得到了以下结果:

White castle: Can't find

Mihilaka Holiday Homes: 070 337 2732


我希望这有帮助!

相关问题