gpt4 book ai didi

python - 从 google places API 获取开放时间

转载 作者:行者123 更新时间:2023-12-05 04:19:17 25 4
gpt4 key购买 nike

我正在使用 this python library 从 google places API 获取响应。

使用上述库中的 places 函数返回您在下面看到的对象。你可以看到这只是给我一个关于餐厅是开门还是关门的 bool 值。 我如何查看一周中每一天的开放时间?如果这个图书馆不支持,谁能给我举个例子?

这是请求完整上下文的代码行。

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 API的Place Details

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

根据文档:

"Place Search requests and Place Details requests do not return the same fields. Place Search requests return a subset of the fields that are returned by Place Details requests. If the field you want is not returned by Place Search, you can use Place Search to get a place_id, then use that Place ID to make a Place Details request."

现在您根据 python library documentation 在您的代码中使用了什么是 places(*args, **kwargs),也就是 Place Search

您在上面的评论中提供的 Google Maps API 文档,您可以获得每天的预期结果来自地点详细信息,即地点(*args, **kwargs) 来自 python library documentation .

如上文所述,要请求地点的详细信息,您需要 place_id ,您可以通过执行 Places Search 请求获得它,就像您在问题中所做的那样。所以您只需要通过Place Search获取您想要的位置的place_id,然后使用该place_id获取Place Details 结果,包括 opening_hours 字段结果。

这是它在 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']}

就这些了,我希望这对您有所帮助!如果这不符合您的预期结果,请随时在下方发表评论。

关于python - 从 google places API 获取开放时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74858622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com