你可以使用 Python 的 Requests 库,将一个或多个公开账号的用户名、URL 或数字用户 ID 发送至 Datablist 的 Instagram Followers Scraper API,抓取 Instagram 粉丝。API 会以 JSON 格式返回粉丝记录,并标明每条关注关系对应的来源账号。

本指南首先抓取长距离徒步者兼电影创作者 Jupiter Hikes 的粉丝。随后,我们会复用同一个 Python 函数,将其粉丝与另一位长距离徒步者兼户外电影创作者 Darwin on the Trail 的粉丝进行比较。代码会按 Instagram 数字用户 ID 对结果分组,找出同时出现在两份已采集列表中的用户。

🔑 时间有限?先看摘要

使用 Datablist 的 Instagram Followers Scraper API,通过 Python 抓取公开 Instagram 账号的粉丝。提交要抓取的账号,为每个账号设置数量上限,再以 JSON 格式获取可用的粉丝记录,用于调研或营销工作流。开始前,请先确认 Instagram 允许你浏览相应账号的粉丝列表。

  • 输入: Instagram 用户名、账号 URL 或数字用户 ID。
  • 输出: 粉丝记录,包括用户 ID、可获取的账号详情,以及每位粉丝对应的来源账号。

💡 更想使用 no-code 方案? 使用 Datablist 的 Instagram Followers source,无需编写代码即可生成粉丝数据行。

基于 collection 抓取 Instagram 粉丝的指南介绍了如何通过 Datablist 界面使用同一数据源。本文其余部分将重点讲解 API。

检查 Instagram 是否限制粉丝列表

提交抓取任务前,请打开目标 Instagram 账号并点击 Followers,确认你能够浏览粉丝列表。账号处于公开状态,并不代表 Instagram 会展示其全部粉丝。

如果看到 “Only [username] can see all followers”,即使账号是公开的,Scraper 通常也只能返回部分列表。API 只能获取 Instagram 实际开放访问的粉丝;提高请求上限并不能解锁隐藏的粉丝。

Levi’s Instagram 账号上的粉丝可见性提示。
Levi’s Instagram 账号上的粉丝可见性提示。

请逐一检查计划抓取的账号,在发起 API 请求前确认其可见粉丝列表是否足以满足你的任务需求。

配置 Python 和 Datablist API Key

Growth 套餐包含 Datablist REST API 访问权限。运行 Source 任务也会消耗 credits。完成操作演示后,我们会详细说明粉丝 Source 的费用。

在 Datablist API 账号页面创建 Personal API Key。本例使用 requests package。新建一个项目文件夹,在终端中打开,然后运行:

shell
python -m venv .venv
source .venv/bin/activate
python -m pip install requests

创建名为 scrape_instagram_followers.py 的文件,然后将 API Key 写入 DATABLIST_API_KEY 环境变量:

shell
export DATABLIST_API_KEY="your-api-key"

脚本会读取此变量,并通过 X-API-Key header 发送该值。

用一个 Python 函数抓取 Instagram 粉丝

第一个请求使用 jupiterhikes,这是长距离徒步者兼内容创作者 Jupiter Hikes 的用户名。该 Source 也支持完整账号 URL 或 Instagram 数字用户 ID 字符串。例如,Jupiter Hikes 的数字 ID 是 "1507518892"。

将以下完整脚本粘贴到 scrape_instagram_followers.py:

python
import json
import os
import time
import uuid
import requests
def scrape_instagram_users(
profiles,
api_key,
scraping_type="followers",
limit_per_profile=100,
):
"""Submit profiles, wait for the source task, and return all generated users."""
tasks_url = "https://api.datablist.com/v1/sources/tasks"
headers = {"X-API-Key": api_key}
unique_id = str(uuid.uuid4())
print(f"Task uniqueId: {unique_id}", flush=True)
payload = {
"sourceId": "instagram-source",
"uniqueId": unique_id,
"settings": {
"profiles": profiles,
"scrapingType": scraping_type,
"limitPerProfile": limit_per_profile,
},
}
response = requests.post(tasks_url, headers=headers, json=payload)
response.raise_for_status()
task = response.json()
task_id = task["@id"]
task_url = f"{tasks_url}/{task_id}"
print(f"Task ID: {task_id}")
while task["status"] not in ("completed", "failed", "stopped"):
time.sleep(3)
response = requests.get(task_url, headers=headers)
response.raise_for_status()
task = response.json()
print(f"Task status: {task['status']}; credits used: {task['credits']['used']}")
if task["status"] != "completed":
raise RuntimeError(
f"Task {task_id} {task['status']}: {task.get('error')}"
)
results = []
params = {"limit": 10}
while True:
response = requests.get(
f"{task_url}/results",
headers=headers,
params=params,
)
response.raise_for_status()
page = response.json()
results.extend(page["results"])
next_cursor = page["nextCursor"]
if next_cursor is None:
break
params["cursor"] = next_cursor
return results
if __name__ == "__main__":
api_key = os.environ.get("DATABLIST_API_KEY")
if not api_key:
raise RuntimeError("Set DATABLIST_API_KEY before running this script.")
profiles = ["jupiterhikes"]
follower_results = scrape_instagram_users(profiles, api_key)
print(json.dumps(follower_results, indent=2, ensure_ascii=False))

运行命令:

shell
python scrape_instagram_followers.py

该函数会依次发起三类 API 调用。

  1. POST /v1/sources/tasks 发送 Source ID 和设置。API 接收后台任务并返回其 task ID。
  2. GET /v1/sources/tasks/{taskId} 每三秒检查一次任务状态。Requests client 会等待,但任务本身在 Datablist 中异步运行。
  3. GET /v1/sources/tasks/{taskId}/results 获取一页扁平化的粉丝对象。循环会原样传回每个非 null 的 nextCursor,直到 API 返回 null。

示例将 scrapingType 设为 followers,并通过 limitPerProfile 请求 100 位用户。这里的 100 是请求上限,而不是精确的结果数。Provider 的分页边界以及任务运行期间可访问的用户都可能影响最终返回数量。

脚本会在提交任务前输出 uniqueId。如果 POST 期间连接中断,可通过该值查找已被接收的任务,避免立即创建另一个付费任务。

API 如何生成粉丝关系记录

Datablist 首先将每个用户名、完整账号 URL 或数字 ID 解析为来源 Instagram 账号。随后,后台任务会获取粉丝分页并处理 Provider cursor。你只需管理脚本中展示的 Datablist 任务 cursor 和结果分页 cursor。

同一来源账号返回的列表中,重复的用户 ID 会被去重。如果同一用户同时出现在两个已提交创作者的粉丝中,API 会保留两条关系记录,每条记录的 followerSourceProfile... 字段不同。正是这一机制让受众比较成为可能。

后续章节会用到以下字段:

  • instagramUserId 是比较返回用户时使用的稳定 key。
  • instagramUsername、instagramFullName 及其他 instagram... 字段用于描述粉丝,前提是相应公开信息可用。
  • followerSourceProfileId、followerSourceProfileUsername 和 followerSourceProfileUrl 用于标识起始创作者账号。

该 Source 用于获取关系列表,不会搜索候选对象,也不会计算置信度分数。

查看 Jupiter Hikes 抓取结果

Jupiter Hikes 任务完成后返回了 124 条不重复的粉丝记录,共消耗 2.24 credits。尽管请求指定了 limitPerProfile: 100,但 Provider 返回下一页 25 位用户前,累计数量为 99。Datablist 保留了这一完整分页,因此最终获得 124 条结果。

以下是最后一页结果的响应。Envelope 和四个粉丝对象均按原始返回内容保留:

json
{
"taskId": "695846750eb440c3870e763d38ef4f4b",
"results": [
{
"instagramUserId": "52256365838",
"instagramUsername": "adventuresoforest",
"instagramFullName": "Forest",
"instagramProfileUrl": "https://www.instagram.com/adventuresoforest/",
"instagramPicture": "https://scontent-fra3-2.cdninstagram.com/v/t51.82787-19/809812326_18068055263741839_8426418313724501534_n.jpg?stp=dst-jpg_e0_s150x150_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InByb2ZpbGVfcGljLmRqYW5nby4xMDgwLmMyIn0&_nc_ht=scontent-fra3-2.cdninstagram.com&_nc_cat=107&_nc_oc=Q6cZ2gGE1JNYkQoszNL29wj7UyG4OGKOarcf_iw1luHzGMllAjP-kLWQ5LQPxtYI3LUcC5g&_nc_ohc=1hyyyEDHulgQ7kNvwFJIKKP&_nc_gid=ATIykjgJSgaEOBqdAnjQcg&edm=AIfao94BAAAA&ccb=7-5&ig_cache_key=GGbBRDCPB8fvzDBAAB4eZa7PpvB0bmNDAQAB1501500j-ccb7-5&oh=00_AQQZ3R0wDeV-bTnR2lYSje_IQ-ly9n6pyxGHXpJGAmzUHA&oe=6AF37F89&_nc_sid=9f8d40",
"followerSourceProfile": "https://www.instagram.com/jupiterhikes/",
"followerSourceProfileId": "1507518892",
"followerSourceProfileUsername": "jupiterhikes",
"followerSourceProfileUrl": "https://www.instagram.com/jupiterhikes/",
"instagramVerified": false,
"instagramPrivate": false
},
{
"instagramUserId": "199102398",
"instagramUsername": "richie_nu_field",
"instagramFullName": "新野 豊",
"instagramProfileUrl": "https://www.instagram.com/richie_nu_field/",
"instagramPicture": "https://scontent-fra3-1.cdninstagram.com/v/t51.2885-19/348434195_2264827180367957_813086592749200065_n.jpg?stp=dst-jpg_e0_s150x150_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InByb2ZpbGVfcGljLmRqYW5nby42NDAuYzIifQ&_nc_ht=scontent-fra3-1.cdninstagram.com&_nc_cat=105&_nc_oc=Q6cZ2gGE1JNYkQoszNL29wj7UyG4OGKOarcf_iw1luHzGMllAjP-kLWQ5LQPxtYI3LUcC5g&_nc_ohc=xux3gQ4SQEAQ7kNvwGj5tEv&_nc_gid=ATIykjgJSgaEOBqdAnjQcg&edm=AIfao94BAAAA&ccb=7-5&ig_cache_key=GBOvxBRVQJAr2QsIAMHicrzyqUgLbkULAAAB1501500j-ccb7-5&oh=00_AQRa3Zl_viAM914UM1_kQYrqGwPLfpmVqPwlMltzkNd44Q&oe=6AF3871F&_nc_sid=9f8d40",
"followerSourceProfile": "https://www.instagram.com/jupiterhikes/",
"followerSourceProfileId": "1507518892",
"followerSourceProfileUsername": "jupiterhikes",
"followerSourceProfileUrl": "https://www.instagram.com/jupiterhikes/",
"instagramVerified": false,
"instagramPrivate": false
},
{
"instagramUserId": "43861505420",
"instagramUsername": "shivroam",
"instagramFullName": "Shivam Rana I Mountaineer",
"instagramProfileUrl": "https://www.instagram.com/shivroam/",
"instagramPicture": "https://scontent-fra3-1.cdninstagram.com/v/t51.82787-19/858921985_18104766389457421_6701325443259882004_n.jpg?stp=dst-jpg_e0_s150x150_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InByb2ZpbGVfcGljLmRqYW5nby4xMDgwLmMyIn0&_nc_ht=scontent-fra3-1.cdninstagram.com&_nc_cat=101&_nc_oc=Q6cZ2gGE1JNYkQoszNL29wj7UyG4OGKOarcf_iw1luHzGMllAjP-kLWQ5LQPxtYI3LUcC5g&_nc_ohc=NMw7vs1oyqQQ7kNvwEYvnsx&_nc_gid=ATIykjgJSgaEOBqdAnjQcg&edm=AIfao94BAAAA&ccb=7-5&ig_cache_key=GAEcMjMNkmJpMFJAABTaykn84-9cbmNDAQAB1501500j-ccb7-5&oh=00_AQR5iJXKSUhS5uS8xmmkgeYw-QZ8AbYxVHqIj3guogZgyA&oe=6AF38C8B&_nc_sid=9f8d40",
"followerSourceProfile": "https://www.instagram.com/jupiterhikes/",
"followerSourceProfileId": "1507518892",
"followerSourceProfileUsername": "jupiterhikes",
"followerSourceProfileUrl": "https://www.instagram.com/jupiterhikes/",
"instagramVerified": false,
"instagramPrivate": false
},
{
"instagramUserId": "23413245087",
"instagramUsername": "double24762",
"instagramFullName": "double 佳",
"instagramProfileUrl": "https://www.instagram.com/double24762/",
"instagramPicture": "https://scontent-iad6-1.cdninstagram.com/v/t51.2885-19/573323465_1219825463302212_7278921664109726296_n.png?stp=dst-jpg_e0_s150x150_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InByb2ZpbGVfcGljLmRqYW5nby4xNTAuYzIifQ&_nc_ht=scontent-iad6-1.cdninstagram.com&_nc_cat=1&_nc_oc=Q6cZ2gH2sHBsdmQY1aWiWoZZWA6eZ603UbEQOH0raGAaH9APRHqzVjuOTqB7gW86Vp6E9Xx44csiK9pjfjrqCVl5R1HC&_nc_ohc=kwiilGbw6cUQ7kNvwHe7b38&edm=AAAAAAABAAAA&ccb=7-5&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.3-ccb7-5&oh=00_AQT5TH4G4DvUg9YUXYlVAW4_1Z03ebdjvl84MLuV0umlaQ&oe=6AF37C2A&_nc_sid=328259",
"followerSourceProfile": "https://www.instagram.com/jupiterhikes/",
"followerSourceProfileId": "1507518892",
"followerSourceProfileUsername": "jupiterhikes",
"followerSourceProfileUrl": "https://www.instagram.com/jupiterhikes/",
"instagramVerified": false,
"instagramPrivate": false
}
],
"nextCursor": null
}

第一条记录的数字用户 ID 是 52256365838,用户名为 adventuresoforest,并包含可获取的公开账号字段。其 followerSourceProfileUsername 为 jupiterhikes,因此你可以判断这条关系来自哪个已提交账号。即使某个可选账号字段缺失,任务仍可正常完成。请读取实际存在的字段,不要按照固定 schema 重新构建每条记录。

按 Instagram 用户 ID 比较两组受众

确认单账号请求正常工作后,在同一个任务中复用 scrape_instagram_users() 抓取两位徒步创作者。按数字用户 ID 对返回的关系记录分组,并保留与该 ID 关联的所有来源用户名:

python
comparison_profiles = [
"jupiterhikes",
"https://www.instagram.com/darwin_onthetrail/",
]
comparison_results = scrape_instagram_users(comparison_profiles, api_key)
audience_by_id = {}
collected_count = {}
for result in comparison_results:
source_username = result["followerSourceProfileUsername"]
collected_count[source_username] = collected_count.get(source_username, 0) + 1
user_id = result["instagramUserId"]
grouped_user = audience_by_id.setdefault(
user_id,
{"user": result, "source_profiles": set()},
)
grouped_user["source_profiles"].add(source_username)
overlap = [
{
**grouped_user["user"],
"source_profiles": sorted(grouped_user["source_profiles"]),
}
for grouped_user in audience_by_id.values()
if len(grouped_user["source_profiles"]) == len(comparison_profiles)
]
overlap_report = {
"collected_count": collected_count,
"overlap_count": len(overlap),
"overlap": overlap,
}
print(json.dumps(overlap_report, indent=2, ensure_ascii=False))

双账号任务为 Jupiter Hikes 返回了 124 条粉丝关系,为 Darwin on the Trail 返回了 46 条。其中有 3 个数字 ID 同时出现在两份已采集列表中。任务共消耗 2.7 credits。

分组代码使用 instagramUserId 而不是用户名,因为 ID 是 API 返回的稳定 join key。代码会保留一个完整的 API 用户对象,并添加排序后的 source_profiles 列表。对于三个共有 ID,该列表都包含 darwin_onthetrail 和 jupiterhikes。

例如,数字 ID 42048393083 在 Jupiter Hikes 和 Darwin on the Trail 的结果中各出现一次。两条记录的用户名都是 hikingful,但各自的 followerSourceProfileUsername 不同。分组代码会将这两条关系记录合并为一个用户对象,并写入 source_profiles: ["darwin_onthetrail", "jupiterhikes"]。

通过数字用户 ID 关联两份已采集的 Instagram 粉丝列表,在保留两个来源账号的同时找出受众重叠
通过数字用户 ID 关联两份已采集的 Instagram 粉丝列表,在保留两个来源账号的同时找出受众重叠

📘 请谨慎解读重叠数据: 共有 ID 只代表该用户出现在两个返回的数据切片中。它不能衡量两位创作者完整受众的重叠程度,不能证明该用户在文章发布时仍保持关注,也不代表该账号适合进行 Outbound 触达。

collected_count 的值明确给出了分母。如果要计算百分比,请清楚标注其含义。例如,3 / 124 表示共有 ID 占本次任务所采集 Jupiter Hikes 记录的比例。

如果你更喜欢在本地浏览器中运行脚本,可以查看免费抓取 Instagram 粉丝、关注列表和帖子作者的指南。

丰富共有账号以开展受众研究

重叠结果提供了 Instagram 数字 ID 和来源信息。接下来,可以将这些共有 ID 提交给 Instagram Profile Scraper enrichment,再根据可用的网站、公开邮箱、bio、类别或账号指标,对成功返回的结果进行分组。

在 Source 函数下方添加第二个函数。它使用同一个 API Key,并沿用相同的提交、轮询和分页流程:

python
def enrich_instagram_profiles(overlap_users, api_key):
"""Retrieve public profile details for the numeric IDs in the overlap."""
if not overlap_users:
return []
tasks_url = "https://api.datablist.com/v1/enrichments/tasks"
headers = {"X-API-Key": api_key}
inputs = [
{
"inputId": user["instagramUserId"],
"data": {"instagramIdentifier": user["instagramUserId"]},
}
for user in overlap_users
]
payload = {
"enrichmentId": "instagram-profile-scraper",
"uniqueId": str(uuid.uuid4()),
"settings": {"scrapeType": "profile"},
"inputs": inputs,
}
response = requests.post(tasks_url, headers=headers, json=payload)
response.raise_for_status()
task = response.json()
task_id = task["@id"]
task_url = f"{tasks_url}/{task_id}"
while task["status"] not in ("completed", "failed", "stopped"):
time.sleep(3)
response = requests.get(task_url, headers=headers)
response.raise_for_status()
task = response.json()
if task["status"] != "completed":
raise RuntimeError(
f"Task {task_id} {task['status']}: {task.get('error')}"
)
profile_results = []
params = {"limit": 100}
while True:
response = requests.get(
f"{task_url}/results",
headers=headers,
params=params,
)
response.raise_for_status()
page = response.json()
profile_results.extend(page["results"])
if page["nextCursor"] is None:
break
params["cursor"] = page["nextCursor"]
return profile_results
profile_results = enrich_instagram_profiles(overlap, api_key)
profiles_with_websites = [
result
for result in profile_results
if result["status"] == "success"
and result["outputs"].get("instagramWebsite")
]
profiles_with_public_email = [
result
for result in profile_results
if result["status"] == "success"
and result["outputs"].get("instagramEmail")
]

如果两份已采集列表没有共有 ID,overlap 将为空,该函数会在发送 POST 请求前返回 []。这样,你仍可继续处理空的研究分组,同时避免创建付费 enrichment 任务。

将每个数字 ID 同时用作 inputId 和 instagramIdentifier,可确保结果能够明确关联回重叠数据。请保留所有返回的结果对象,包括未成功的状态、空 outputs、credits 用量和错误信息。只有在本地建立分组时再进行过滤。

profile 模式的起步费用为每个数字 ID 2 credits。若账号公开了相关信息,它可以返回 bio、网站、邮箱或电话号码,以及账号统计数据和类别详情。即使成功找到账号,联系字段也可能为空。代码只会在本地准备研究分组,不会发送消息,也不会把共有粉丝视为同意接受联系。

如需 no-code 版本,可使用 Datablist 的 Instagram Profile Scraper。

从抓取粉丝切换为抓取关注列表

同一 Source 也能获取指定账号所关注的其他账号。只需修改 scraping_type 参数:

python
following_results = scrape_instagram_users(
["1507518892"],
api_key,
scraping_type="following",
limit_per_profile=100,
)

这些记录描述的是 Jupiter Hikes 关注的账号。来源字段会变为 followingSourceProfileId、followingSourceProfileUsername 和 followingSourceProfileUrl。与 followers 模式一样,请以实际返回数量为准。

估算 Instagram 粉丝 API 费用

Instagram 粉丝 Source 每返回一位粉丝或被关注用户,会消耗 0.01 credits。解析每个已提交的来源账号最多还会消耗 1 credit。若命中缓存,实际费用可能更低。

对于两个账号,若每个账号请求上限为 100 位用户,可按以下公式预估:

2 × (1 + 100 × 0.01) = 4 credits

这只是预估值,并非费用上限。该双账号任务最终返回了 170 条关系记录,实际使用 2.7 credits。任务完成后,请读取 task.credits.used 获取真实费用。

Loading pricing…

API 访问权限、套餐内 credits、可选充值和任务用量是彼此独立的。Growth 月度套餐价格为 50 美元或 50 欧元,包含 REST API 访问权限和每月 20,000 credits。目前最低充值档为 20 美元或 17.50 欧元,可获得 20,000 credits。

按照上述充值费率,返回 1,000 位用户会消耗 10 credits。在不计来源账号解析费用的情况下,对应价值为 0.01 美元或 0.00875 欧元。这是标准化后的用量价值,并不是可单独购买的套餐:充值仍需支付 20 美元或 17.50 欧元,而 API 访问仍要求订阅 Growth 套餐。请查看最新的 Datablist 定价了解完整目录。

可选的 Instagram Profile Scraper 会单独计费。使用数字 ID 且设置 scrapeType: "profile" 时,每个已提交的共有账号起步价为 2 credits。

解决常见的输入、任务和结果问题

API Key 缺失

脚本会在发出 HTTP 请求前抛出 Set DATABLIST_API_KEY before running this script.。请在同一个 shell 中导出该变量,然后重新运行文件。

API 拒绝 API Key

response.raise_for_status() 会抛出 HTTP 错误。请确认 Personal API Key 复制无误,并检查账号是否拥有 Growth 套餐的 API 访问权限。继续使用示例中的单个 X-API-Key header。

无法解析来源账号

请确认每个值都是用户名、完整 Instagram 账号 URL 或数字用户 ID 字符串。创建另一个付费任务前,先检查现有任务及其错误信息。

Credits 不足

提交新任务前,请检查余额,并减少账号数量或请求上限。已经启动的任务仍可能消耗 credits。

任务失败或已停止

脚本会输出 task ID 并抛出 RuntimeError。使用该 ID 检查现有任务的 error、credits.used 及所有可用的部分结果。失败或停止的任务仍可能保留已生成的数据行,因此请先完成检查,再次提交任务。

返回数量与请求上限不同

请报告结果中的实际数量。limitPerProfile 只是请求上限,并不保证返回精确数量;仅凭一次结果也无法判断 Provider 为何返回了更多或更少的可访问用户。

提交响应丢失

列出近期 Source 任务,并匹配 POST 前输出的 uniqueId:

python
unique_id = "paste-the-printed-unique-id"
response = requests.get(
"https://api.datablist.com/v1/sources/tasks",
headers={"X-API-Key": api_key},
params={"sourceId": "instagram-source", "limit": 100},
)
response.raise_for_status()
for task in response.json()["tasks"]:
if task["uniqueId"] == unique_id:
print(task["@id"], task["status"])

继续轮询该 task ID,或获取其已有结果。Sources 任务文档详细介绍了任务列表、状态、结果和数据保留 endpoints。

后续步骤

现在,你已经拥有一个可复用的 Python Requests 函数,用于获取 Instagram 粉丝关系;同时还获得了一份本地报告,可按数字用户 ID 比较已采集的 Jupiter Hikes 与 Darwin on the Trail 粉丝列表。每条共有记录都会保留两个来源账号。

接下来,可将示例替换为一小组经过审核的公开创作者账号。为每个账号设置合理的抓取上限,再根据各来源的实际返回数量解读受众重叠。你可以下载持续维护的同步 Requests 示例;如果需要非阻塞并发,也可以使用持续维护的异步 HTTPX 示例。如果还需要先寻找来源账号,请从按关键词查找 Instagram 账号的指南开始。