Instagram按短代码获取帖子评论异常及完整请求咨询
Hey there, let's tackle this issue step by step. First off, this is not an Instagram bug—it's actually how their GraphQL API handles large comment sets via pagination, plus some common pitfalls with how you're structuring your requests.
Why the comment count doesn't match the list
Instagram intentionally limits the number of comments returned in a single API call, even if you set a high first value like 4500. There's a hard cap on how many comments you can fetch per request (usually around 100-200, though this can vary), so your initial request only gets a chunk of the total comments, not all of them. The "total comments" field in the response reflects the full count, but you need to paginate through subsequent requests to collect everything.
What's wrong with your current requests
Your second request has a malformed parameter structure. The after cursor shouldn't be a top-level URL parameter—it needs to be included inside the variables JSON object, just like shortcode and first. Instagram's GraphQL API expects all pagination-related parameters to be part of the variables payload, not appended directly to the URL.
Correct way to fetch the full comment list
Here's a step-by-step approach to get every comment:
Initial request
Start with a valid initial query to get the first batch of comments, along with pagination metadata:https://www.instagram.com/graphql/query/?query_id=17852405266163336&variables={"shortcode":"BemdtV0gLAL","first":100}(Note: Using
first:100is more reliable than 4500, since Instagram will cap it anyway.)In the response, look for the
page_infoobject underdata.shortcode_media.edge_media_to_parent_comment:end_cursor: This is the token you'll use to fetch the next page of commentshas_next_page: A boolean that tells you if there are more comments to fetch
Paginate through remaining comments
Whilehas_next_pageistrue, make subsequent requests by adding theend_cursorto your variables:https://www.instagram.com/graphql/query/?query_id=17852405266163336&variables={"shortcode":"BemdtV0gLAL","first":100,"after":"[INSERT_END_CURSOR_HERE]"}Replace
[INSERT_END_CURSOR_HERE]with theend_cursorvalue from the previous response. Repeat this untilhas_next_pagebecomesfalse.Combine all results
Collect theedgesarray from each response and merge them into a single list—this will give you the full set of comments that matches the total count.
Important notes
- Query ID changes: Instagram regularly updates
query_idvalues, so if your current one stops working, you'll need to find the latest valid ID (you can get this by inspecting network traffic when loading comments on Instagram's web interface). - Rate limiting: Be careful not to send too many requests too quickly—Instagram will block your IP if you hit their rate limits. Add small delays between pagination requests.
- Hidden comments: In some cases, the total count might include comments that are filtered (e.g., spam, restricted content) and won't be returned in the API response. If you've paginated until
has_next_pageisfalseand still don't match the total, this could be the reason.
内容的提问来源于stack exchange,提问作者Alexey Gordienko

