从谷歌地点API获取营业时间

3

我正在使用这个Python库从Google Places API获取响应。

使用上述库中的places函数返回下面看到的对象。您可以看到它只给出了餐厅是否开放的布尔值。如何查看每周每天的营业时间?如果此库无法实现,请问有人可以向我展示一个例子吗?

以下是发出完整请求的代码行。

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'

如果可能的话,我想使用Google Places API。Google有文档显示返回一个对象,其中包含每天的营业时间,但是它是用JavaScript编写的。 - Justin Priede
这是我参考的文档:https://developers.google.com/maps/documentation/places/web-service/details - Justin Priede
请查看此答案 - https://stackoverflow.com/a/50747378/19813684 - elebur
1
@elebur,这不是OP想要的。他们想要获取企业的每周时间表,这实际上是根据OP在其上面评论中提供的文档链接的Web服务的结果。 - Yrll
1
为了明确,我完全可以使用不同的库/方法。我想留在Python中,但我并不局限于这个库。 - Justin Priede
1
你使用的库很好。我能够用它提供一个解决方案。你应该看看它。 - Yrll
1个回答

2

使用的是Places API的Place Details

在我们继续解决方案之前,我们必须了解Place SearchPlace Details请求结果的差异。

根据文档:

"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 Maps API文档,其中可以获得每天的预期结果是来自Place Details,即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']}

这就是全部内容,希望能对你有所帮助!如果这份翻译未达到你的预期,请在下方评论区留言。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接