以下是一个示例代码,展示了如何存储Google Places的place_id并刷新它们:
import requests
import json
# 定义Google Places API的基本URL和API密钥
base_url = 'https://maps.googleapis.com/maps/api/place/details/json'
api_key = 'YOUR_API_KEY'
# 定义要存储的place_id列表
place_ids = ['PLACE_ID_1', 'PLACE_ID_2', 'PLACE_ID_3']
# 循环遍历每个place_id
for place_id in place_ids:
# 构建API请求URL
url = f'{base_url}?place_id={place_id}&key={api_key}'
# 发送API请求
response = requests.get(url)
# 解析API响应
data = json.loads(response.text)
# 检查API响应是否成功
if data['status'] == 'OK':
# 从API响应中提取所需的数据
place_details = data['result']
# 在此处执行刷新逻辑,例如更新数据库或执行其他操作
print(f'Refreshing place with place_id: {place_id}')
print(f'Place name: {place_details["name"]}')
else:
# 处理API响应错误
print(f'Error fetching place with place_id: {place_id}')
print(f'Error status: {data["status"]}')
请确保将YOUR_API_KEY
替换为您自己的Google Places API密钥,并将PLACE_ID_1
,PLACE_ID_2
和PLACE_ID_3
替换为您要存储和刷新的实际place_id。