如何通过Youtube Python Data API获取视频的发布日期?
通过YouTube Python Data API获取视频发布日期的方法
完全可行,你需要指定part=snippet参数——视频的发布日期就包含在snippet返回结果的publishedAt字段里。
实现步骤与代码示例
- 先安装官方的Python客户端库:
pip install google-api-python-client
- 编写代码调用API:
from googleapiclient.discovery import build from datetime import datetime # 替换成你的API密钥 API_KEY = "your_api_key_here" VIDEO_ID = "目标视频ID" # 构建API服务 youtube = build('youtube', 'v3', developerKey=API_KEY) # 调用videos.list接口 request = youtube.videos().list( part="snippet", id=VIDEO_ID ) response = request.execute() # 提取并格式化发布日期 if response['items']: published_at_str = response['items'][0]['snippet']['publishedAt'] # 转换为本地时间格式 published_date = datetime.fromisoformat(published_at_str.replace('Z', '+00:00')).astimezone() print(f"视频发布日期:{published_date.strftime('%Y-%m-%d %H:%M:%S')}") else: print("未找到对应视频")
返回的publishedAt默认是ISO 8601格式的UTC时间字符串,代码里额外做了格式转换,你可以根据需求调整输出样式。
内容的提问来源于stack exchange,提问作者David Tomić
相关产品推荐
相关产品推荐

