You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的Instaloader库下载带声音的Instagram Reel?

解决Instaloader下载Instagram Reel时的KeyError: 'date_utc'问题

问题描述

尝试用Python的Instaloader库下载带声音的Instagram Reel,代码如下:

import instaloader
L = instaloader.Instaloader()
post = instaloader.Post.from_shortcode(L.context, "https://www.instagram.com/reel/CNr9_PnJXLw/")
video_url = post.video_url
filename = L.format_filename(post, target=post.owner_username)
L.download_pic(filename=filename, url=video_url, mtime=post.date_utc)

运行时触发KeyError: 'date_utc'错误,错误详情(中文翻译):

文件 "/Users/vin/Desktop/testing/.venv/lib/python3.7/site-packages/instaloader/instaloader.py",第697行,在download_post函数中:
filename_template = os.path.join(dirname, self.format_filename(post, target=target))

问题原因

  1. 参数错误:Post.from_shortcode的第二个参数应传入Reel的shortcode(即CNr9_PnJXLw),而非完整URL。
  2. 方法误用:download_pic是专门用于下载图片的方法,不适合处理视频,调用时会因内部依赖图片专属字段导致报错。
  3. 手动处理冗余:Instaloader提供了更简便的视频处理方法,无需手动提取URL和文件名。

修正后的代码

import instaloader

L = instaloader.Instaloader()
# 传入Reel的shortcode而非完整URL
post = instaloader.Post.from_shortcode(L.context, "CNr9_PnJXLw")
# 使用download_post自动处理带音频的视频下载
L.download_post(post, target=post.owner_username)

说明

download_post方法会自动识别Post类型(Reel属于视频Post),下载带声音的完整视频文件,同时自动处理文件名、时间戳等细节,避免手动处理时的错误。

内容的提问来源于stack exchange,提问作者Vijay Chauhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 03:42:17