import requests
import json
假设的API端点(这不是抖音的API,只是一个示例)
API_ENDPOINT = "https://api.example.com/shops/new"
假设的API密钥(在实际应用中,你应该从安全的地方获取这个密钥)
API_KEY = "your_api_key_here"
请求头,包含API密钥
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
发送GET请求到假设的API端点
def fetch_new_shops():
try:
response = requests.get(API_ENDPOINT, headers=headers)
response.raise_for_status() # 如果请求失败,将引发HTTPError异常
# 解析JSON响应
data = response.json()
# 假设响应包含一个名为'shops'的列表,每个元素都是一个店铺字典
shops = data.get('shops', [])
# 打印每个店铺的信息
for shop in shops:
print(f"店铺名称: {shop['name']}")
print(f"店铺ID: {shop['id']}")
print(f"开店时间: {shop['created_at']}")
print("-" * 40)
return shops
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return []
if name == "main":
new_shops = fetch_new_shops()
# 你可以在这里对new_shops列表进行进一步的处理,比如保存到数据库或文件