Я пытаюсь использовать два вызова API Google, чтобы получить price_level
и номер телефона ресторана.
Во-первых, цикл через
for restaurant in name:
find_place_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
# use separate parameter dictionary b.c. findplace and findplacedetail have diff field.
find_place_param ={}
find_place_param["input"] = restaurant
find_place_param["inputtype"] = "textquery"
find_place_param["key"] = google_key
# get place_id then use it to get phone number
a = requests.get(find_place_url, parameters).json()
это первый API-интерфейс findplace, используемый для получения place_id
для данного ресторана. Это будет выглядеть так:
{'candidates': [{'place_id': 'ChIJdTDCTdT4cUgRqxush2XhgnQ'}], 'status': 'OK'}
, если у данного ресторана есть place_id
, иначе он даст:
{'candidates': [], 'status': 'ZERO_RESULTS'}
теперь это весь мой код: отсюда я беру place_id
, но попробую и за исключением того, что, как указано выше, статус либо ноль, либо ок. Но даже если я пройду мимо, за исключением того, что он выполнит вызов API find_place_detail, который требует place_id, таким образом, он завершится неудачно. Как я могу пропустить последний блок кода, если я не получаю place_id?
price_level2 = []
phone_number = []
for restaurant in name:
find_place_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
# use separate parameter dictionary b.c. findplace and findplacedetail have diff field.
find_place_param ={}
find_place_param["input"] = restaurant
find_place_param["inputtype"] = "textquery"
find_place_param["key"] = google_key
# get place_id then use it to get phone number
a = requests.get(find_place_url, parameters).json()
print(a)
# adding it to original parameter. since only this and findplace parameter has to be different.
try:
parameters["place_id"] = a["candidates"][0]["place_id"]
except:
print("Phone number not available")
phone_number.append(None)
# passing in fields of our interest
parameters["fields"] = "name,price_level,formatted_phone_number"
find_place_detail_url ="https://maps.googleapis.com/maps/api/place/details/json?"
b = requests.get(find_place_detail_url, parameters).json()
phone_number.append(b["result"]["formatted_phone_number"])
price_level2.append(b["result"]['price_level'])