簡體   English   中英

從 google places 獲取開放時間 API

[英]Grabbing opening hours from google places API

我正在使用這個 python 庫來獲取來自谷歌位置 API 的響應。

使用上述庫中的位置 function 返回您在下面看到的 object。 你可以看到這只是給我一個 boolean,關於餐廳是開着還是關着。 如何查看一周中每一天的營業時間? 如果這個庫沒有能力,誰能給我舉個例子?

這是請求完整上下文的代碼行。

import googlemaps # https://googlemaps.github.io/google-maps-services-python/docs/index.html
gmaps = googlemaps.Client(key='apiKey')
response = gmaps.places(query=charlestonBars[idx], location=charleston)
[   {   'business_status': 'OPERATIONAL',
        'formatted_address': '467 King St, Charleston, SC 29403, United States',
        'geometry': {   'location': {'lat': 32.7890988, 'lng': -79.9386229},
                        'viewport': {   'northeast': {   'lat': 32.79045632989271,
                                                         'lng': -79.93725907010727},
                                        'southwest': {   'lat': 32.78775667010727,
                                                         'lng': -79.93995872989272}}},
        'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/bar-71.png',
        'icon_background_color': '#FF9E67',
        'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/bar_pinlet',
        'name': "A.C.'s Bar & Grill",
------->'opening_hours': {'open_now': True},
        'photos': [   {   'height': 1816,
                          'html_attributions': [   '<a '
                                                   'href="https://maps.google.com/maps/contrib/106222166168758671498">Lisa '
                                                   'Royce</a>'],
                          'photo_reference': 'ARywPAKpP_eyNL_y625xWYrQvSjAI91TzEx4XgT1rwCxjFyQjEAwZb2ha9EgE2RcKJalrZhjp0yTWa6QqvPNU9c7GeNBTDtzXVI0rHq2RXtTySGu8sjcB76keFugOmsl1ix4NnDVh0NO0vt_PO3nIZ-R-ytOzzIRhJgPAJd3SxKNQNfEyVp5',
                          'width': 4032}],
        'place_id': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
        'plus_code': {   'compound_code': 'Q3Q6+JG Charleston, South Carolina',
                         'global_code': '8742Q3Q6+JG'},
        'price_level': 1,
        'rating': 4.3,
        'reference': 'ChIJk6nSrWt6_ogRE9KiNXVG5KA',
        'types': [   'bar',
                     'restaurant',
                     'point_of_interest',
                     'food',
                     'establishment'

你需要用到的是Places的Place Details API

在我們繼續解決方案之前,我們必須了解地點搜索地點詳細信息請求的結果是不同的。

根據文檔:

" Place Search請求和Place Details請求不返回相同的字段。Place Search 請求返回由 Place Details 請求返回的字段的子集。如果 Place Search 沒有返回您想要的字段,您可以使用 Place Search 來獲取place_id ,然后使用該 Place ID 發出 Place Details 請求。”

現在,根據python 庫文檔,您在代碼中使用的是places(*args, **kwargs) ,即Place Search

您在上面的評論中提供的 Google 地圖 API 文檔,您可以獲得每天幾小時的預期結果來自Place Details ,它是python 庫文檔中的place(*args, **kwargs)

如上所述,要請求一個地方的詳細信息,您需要place_id ,您可以像在問題上所做的那樣通過執行Places Search請求來獲得它。 因此,您只需要通過Place Search獲取所需位置的place_id ,然后使用該place_id獲取包含opening_hours字段結果的Place Details結果。

這是它在 python 代碼上的樣子:

# I used pprint to print the result on the console

from pprint import pprint
import googlemaps #import googlemaps python library

# Instantiate the client using your own API key

API_KEY = 'API_KEY'
map_client = googlemaps.Client(API_KEY)

# Store the location you want, in my case, I tried using 'Mall of Asia'

location_name = 'Mall of Asia'

# Instantiate Place Search request using `places(*args, **kwargs)` from the library
# Use the `stored location` as an argument for query

place_search = map_client.places(location_name)

# Get the search results

search_results = place_search.get('results')

# Store the place_id from the result to be used

place_id = (search_results[0]['place_id'])

# Instantiate Place Details request using the `place(*args, **kwargs)` from the library
# Use the stored place_id as an argument for the request

place_details = map_client.place(place_id)

# Get the Place Details result

details_results = place_details.get('result')

# Print the result specifying what you only need which is the `opening_hours` field

pprint(details_results['opening_hours'])

此示例請求的結果將是這樣的:

{'open_now': False,
 'periods': [{'close': {'day': 0, 'time': '2200'},
              'open': {'day': 0, 'time': '1000'}},
             {'close': {'day': 1, 'time': '2200'},
              'open': {'day': 1, 'time': '1000'}},
             {'close': {'day': 2, 'time': '2200'},
              'open': {'day': 2, 'time': '1000'}},
             {'close': {'day': 3, 'time': '2200'},
              'open': {'day': 3, 'time': '1000'}},
             {'close': {'day': 4, 'time': '2200'},
              'open': {'day': 4, 'time': '1000'}},
             {'close': {'day': 5, 'time': '2200'},
              'open': {'day': 5, 'time': '1000'}},
             {'close': {'day': 6, 'time': '2200'},
              'open': {'day': 6, 'time': '1000'}}],
 'weekday_text': ['Monday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Tuesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Wednesday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Thursday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Friday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Saturday: 10:00\u202fAM\u2009–\u200910:00\u202fPM',
                  'Sunday: 10:00\u202fAM\u2009–\u200910:00\u202fPM']}

僅此而已,我希望這會有所幫助。 如果這不符合您的預期結果,請隨時在下方發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM