You need to enable JavaScript to run this app. Instagram API如何批量获取账号已关注用户的个人简介
优惠活动
大模型
产品
解决方案
定价
更多

Instagram API如何批量获取账号已关注用户的个人简介

已关注用户列表及对应Bio批量提取实现方案
  • 当前Instagram官方开放API确实未提供「获取自身账号已关注列表」的权限Scope,2022年后大量第三方绕开方案因接口加签、反爬规则升级全部失效,目前可落地的实现路径全部基于自身账号的合法登录态,复用Instagram网页端自带的内部查询接口完成,不需要依赖过时的第三方封装库。

前置准备工作

  • 首先修正现有单用户Bio拉取代码的鉴权缺陷:无Cookie的裸请求现在已经会被Instagram直接返回403拦截,需要先获取自身账号的有效登录态参数
  • 具体参数获取方式:在浏览器完成自身Instagram账号登录,打开开发者工具的网络面板,从任意网页请求的Cookie字段中,复制sessionid、csrftoken、ds_user_id三个核心值,同时复制当前浏览器对应的真实User-Agent字符串,作为后续所有请求的通用配置

第一步:全量拉取自身账号的已关注用户列表

  • 直接调用Instagram网页端自身加载关注列表用的GraphQL查询端点,不需要额外计算接口签名,携带前置准备的登录态即可正常返回数据
  • 分页逻辑:通过游标参数after逐页拉取,单页最多返回50个关注用户,循环直到接口返回的分页字段标记为无下一页为止,过程中提取每个用户的username字段存入本地列表

第二步:批量拉取列表内所有用户的Bio信息

  • 原有单用户Bio拉取逻辑在补上登录态Cookie后,?__a=1端点仍可正常返回对应数据,不需要额外调整解析逻辑
  • 反爬规避规则:
    • 单用户Bio请求的间隔设置为3-5秒,高频连续请求会触发临时IP封禁
    • 请求头不要添加多余的自定义字段,仅保留真实User-Agent和登录Cookie即可,多余字段会提高反爬触发概率

核心可运行代码框架

import requests
import time

# 替换为浏览器中获取的真实登录参数
COOKIES = {
    "sessionid": "你的sessionid值",
    "csrftoken": "你的csrftoken值",
    "ds_user_id": "你的自身数字用户ID值"
}
HEADERS = {
    "User-Agent": "你的浏览器真实User-Agent字符串"
}

def fetch_all_followed_usernames():
    followed_list = []
    next_cursor = ""
    while True:
        request_params = {
            "query_hash": "dbdbfe9c5215dce00061400874c2527d",
            "variables": f'{{"id":"{COOKIES["ds_user_id"]}","include_reel":true,"fetch_mutual":false,"first":50,"after":"{next_cursor}"}}'
        }
        resp = requests.get(
            "https://www.instagram.com/graphql/query/",
            headers=HEADERS,
            cookies=COOKIES,
            params=request_params
        )
        page_result = resp.json()["data"]["user"]["edge_follow"]
        # 提取当前页所有关注用户的用户名
        for item in page_result["edges"]:
            followed_list.append(item["node"]["username"])
        # 无下一页时终止循环
        if not page_result["page_info"]["has_next_page"]:
            break
        next_cursor = page_result["page_info"]["end_cursor"]
        time.sleep(2)
    return followed_list

def fetch_target_user_bio(username):
    resp = requests.get(
        f"https://www.instagram.com/{username}/?__a=1",
        headers=HEADERS,
        cookies=COOKIES
    )
    return resp.json()["graphql"]["user"]["biography"]

if __name__ == "__main__":
    all_followed = fetch_all_followed_usernames()
    user_bio_map = {}
    for username in all_followed:
        user_bio_map[username] = fetch_target_user_bio(username)
        time.sleep(3)
    print(user_bio_map)

落地注意事项

  • 避免使用公开免费代理池执行流程,Instagram对数据中心IP段的封禁率超过90%,优先使用家庭宽带本地IP运行
  • 接口返回空值时优先检查登录Cookie是否过期,重新从浏览器复制最新Cookie值即可解决
  • 单账号关注量超过1000时,建议拆分流程分时段执行,避免一次性批量请求触发账号临时操作限制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.07 18:06:08