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

如何获取Instagram帖子评论?变更后能否通过JSON获取超35条评论?

嘿,我来帮你梳理清楚怎么获取Instagram帖子评论,以及你关心的超过35条评论的问题!

获取Instagram帖子评论的方法及评论数量限制说明

一、基础获取方式(单页最多35条评论)

你给出的代码思路是对的——利用Instagram网页端隐藏的__a=1参数触发JSON数据接口,直接拉取帖子的评论信息。我帮你补全代码里的小疏漏,并优化一下可读性:

import json
import urllib.request

def fetch_instagram_comments(post_link):
    # 拼接带JSON数据接口的请求链接
    json_link = post_link + '?__a=1'
    try:
        # 请求并解析JSON数据
        raw_json = urllib.request.urlopen(json_link).read().decode('UTF-8')
        parsed_data = json.loads(raw_json)
        # 提取评论列表
        comment_edges = parsed_data['graphql']['shortcode_media']['edge_media_to_comment']['edges']
        
        # 打印每条评论的具体内容(可选)
        for comment in comment_edges:
            print(f"评论者: {comment['node']['owner']['username']} | 内容: {comment['node']['text']}")
        return comment_edges
    except Exception as e:
        print(f"获取评论失败: {str(e)}")
        return []

def main():
    # 替换为你需要获取的帖子链接
    fetch_instagram_comments(post_link='https://www.instagram.com/p/BhfVd1env3K/')

if __name__ == "__main__":
    main()

这个方法默认只能拿到最多35条评论,也就是评论列表的第一页数据。

二、获取超过35条评论的解决方案(应对Instagram的接口变更)

Instagram早就调整了接口限制,单靠__a=1只能拿到第一页评论。想要获取全部评论,需要通过**分页游标(cursor)**来循环请求下一页数据:

  1. 第一次请求后,在返回的JSON里找到edge_media_to_comment下的page_info字段,其中end_cursor是下一页评论的游标标识,has_next_page会告诉你是否还有更多评论。
  2. 把游标拼接到请求链接里(格式:post_link?__a=1&max_id=END_CURSOR),再次请求就能拿到下一页评论。

下面是支持获取所有评论的升级代码:

import json
import urllib.request
import time

def fetch_all_instagram_comments(post_link):
    all_comments = []
    next_cursor = None
    base_request_url = post_link + '?__a=1'

    while True:
        # 拼接带游标(如果存在)的请求链接
        current_url = f"{base_request_url}&max_id={next_cursor}" if next_cursor else base_request_url
        try:
            raw_json = urllib.request.urlopen(current_url).read().decode('UTF-8')
            parsed_data = json.loads(raw_json)
            comment_section = parsed_data['graphql']['shortcode_media']['edge_media_to_comment']
            
            # 将当前页评论加入总列表
            all_comments.extend(comment_section['edges'])
            
            # 检查是否还有下一页评论
            page_info = comment_section['page_info']
            if not page_info['has_next_page']:
                break
            
            # 更新游标,准备请求下一页
            next_cursor = page_info['end_cursor']
            # 加延迟避免触发Instagram的请求频率限制
            time.sleep(2)
        except Exception as e:
            print(f"请求过程出错: {str(e)}")
            break
    
    # 打印所有评论(可选)
    for idx, comment in enumerate(all_comments, 1):
        print(f"第{idx}条 | 评论者: {comment['node']['owner']['username']} | 内容: {comment['node']['text']}")
    return all_comments

def main():
    fetch_all_instagram_comments(post_link='https://www.instagram.com/p/BhfVd1env3K/')

if __name__ == "__main__":
    main()

额外注意事项

  • 请求频率控制:Instagram会对频繁请求的IP进行限制,一定要加延迟(比如代码里的time.sleep(2)),避免被临时封禁。
  • 私密内容限制:如果帖子来自私密账号,或者评论需要登录才能查看,这种无登录的请求方式会失效,此时需要模拟登录(比如用requests库携带登录后的Cookie)。
  • 接口稳定性:__a=1属于Instagram的非公开接口,随时可能调整参数或返回结构,如果后续出现解析失败,需要重新查看接口返回的JSON结构调整代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:38:21