在活动启动前检查 IG 账号,有助于及时发现投放名单中的错误。例如,简报中可能有用户名拼写错误、重复账号,或关注者数量异常的个人资料。下面的 Python 脚本会逐一请求用户名对应的公开数据,将响应与设定条件进行比较,并把可核查的记录保存为 CSV。
将生成什么结果
输入是包含账号列表和可选预期值的 CSV。输出中,每个处理过的用户名都有一行记录:请求状态、个人资料名称、认证标记、关注者数量、帖子数量和关注数量,以及简要的差异列表。

这类报告适用于需要依据简报核对合作方的营销人员,也适用于将内部检查构建为定期任务的开发者。它只核查请求时公开个人资料中的特定字段;账号是否符合活动要求,仍由团队人工决定。
可以检查哪些字段
IG API 文档列出的路由 /v1/user/by/username 接受必填参数 username。在观察到的响应格式中,个人资料数据位于 ig 对象内;返回字段包括 username、full_name、is_private、is_verified、media_count、follower_count、following_count 和 biography。字段可能缺失,因此下面的脚本会将缺失值记为空,不会将其转换为零。路由和参数名称列表见IG API 参考文档。
对于能够明确表达的条件,这种检查尤其直观:个人资料必须为公开状态,关注者数量须在指定范围内,认证标记须符合简报要求。个人简介可以保存下来供后续查看,但要将其文本与营销定位进行自动比对,还需要另行制定标准。
准备输入文件
创建一个 UTF-8 编码的 profiles.csv 文件。最简格式只包含 username;如果简报没有相关限制,其他预期字段可留空:
username,min_followers,max_followers,verified
brand_example,5000,50000,false
creator_example,10000,,true
min_followers 和 max_followers 用于设定检查范围。字段 verified 接受 true 或 false。每个个人资料只需填写适用的条件。
请求数据并生成报告
安装 requests,并在进程环境中设置密钥,而不要将其写入源文件。在 macOS 或 Linux 上,命令如下:

python -m pip install requests
export JSONSCRAPER_INSTAGRAM_LICENSE_KEY='你的密钥'
将下面的脚本保存为 check_profiles.py。它适用于 Python 3.8 及更高版本,会检查 HTTP 响应状态,单独读取 ig 数据,并生成 profile_check.csv。密钥通过查询参数 license_key 传递,这与服务文档中的说明一致。
import csv
import os
import sys
from typing import Dict, List, Optional
import requests
API_URL = "https://ig.jsonscraper.com/v1/user/by/username"
API_KEY = os.environ.get("JSONSCRAPER_INSTAGRAM_LICENSE_KEY")
if not API_KEY:
raise SystemExit("请设置 JSONSCRAPER_INSTAGRAM_LICENSE_KEY")
def parse_bool(value: str) -> Optional[bool]:
normalized = value.strip().lower()
if normalized in ("true", "1", "yes", "да"):
return True
if normalized in ("false", "0", "no", "нет"):
return False
return None
def check_row(row: Dict[str, str]) -> Dict[str, str]:
username = row.get("username", "").strip().lstrip("@")
result = {
"requested_username": username,
"status": "",
"returned_username": "",
"full_name": "",
"is_private": "",
"is_verified": "",
"follower_count": "",
"media_count": "",
"following_count": "",
"biography": "",
"issues": "",
}
if not username:
result["status"] = "skipped_empty_username"
result["issues"] = "empty_username"
return result
try:
response = requests.get(
API_URL,
params={"license_key": API_KEY, "username": username},
timeout=30,
)
response.raise_for_status()
payload = response.json()
except (requests.RequestException, ValueError) as exc:
result["status"] = "request_error"
result["issues"] = type(exc).__name__
return result
if payload.get("status") != "ok":
result["status"] = "api_error"
result["issues"] = "unexpected_outer_status"
return result
profile = payload.get("ig")
if not isinstance(profile, dict):
result["status"] = "missing_profile_data"
result["issues"] = "missing_ig_object"
return result
result["status"] = "ok"
fields = (
"username", "full_name", "is_private", "is_verified",
"follower_count", "media_count", "following_count", "biography",
)
for field in fields:
value = profile.get(field)
if value is not None:
result["returned_username" if field == "username" else field] = str(value)
issues: List[str] = []
private = profile.get("is_private")
if private is True:
issues.append("private_profile")
followers = profile.get("follower_count")
if isinstance(followers, int):
minimum = row.get("min_followers", "").strip()
maximum = row.get("max_followers", "").strip()
if minimum:
try:
if followers < int(minimum):
issues.append("below_min_followers")
except ValueError:
issues.append("invalid_min_followers_rule")
if maximum:
try:
if followers > int(maximum):
issues.append("above_max_followers")
except ValueError:
issues.append("invalid_max_followers_rule")
elif row.get("min_followers", "").strip() or row.get("max_followers", "").strip():
issues.append("follower_count_unavailable")
expected_verified = parse_bool(row.get("verified", ""))
actual_verified = profile.get("is_verified")
if expected_verified is not None and isinstance(actual_verified, bool):
if actual_verified != expected_verified:
issues.append("verified_flag_mismatch")
elif row.get("verified", "").strip():
issues.append("verified_value_unavailable_or_invalid")
result["issues"] = ";".join(issues)
return result
with open("profiles.csv", newline="", encoding="utf-8-sig") as source:
rows = list(csv.DictReader(source))
output = [check_row(row) for row in rows]
fieldnames = list(output[0].keys()) if output else [
"requested_username", "status", "returned_username", "full_name",
"is_private", "is_verified", "follower_count", "media_count",
"following_count", "biography", "issues",
]
with open("profile_check.csv", "w", newline="", encoding="utf-8") as destination:
writer = csv.DictWriter(destination, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(output)
print(f"已处理行数:{len(output)};报告:profile_check.csv")
如何阅读差异记录
运行 python check_profiles.py。在 status 列中,ok 表示脚本收到了预期的个人资料对象,并成功生成记录。issues 列只列出已检查的规则,例如 private_profile 或 below_min_followers。该列为空表示记录的条件未发现差异;这并不代表内容质量合格,也不代表符合合同中的其他条款。
请将 returned_username 与输入的 username 核对,并把 biography 和 full_name 交由负责人员进行语义审查。请求错误、意外的 API 状态以及缺少 ig 对象也会记录在报告中:这些记录仍保留在结果里,便于区分技术故障和检查结果。
如何定期运行检查
一次性检查只需手动运行脚本。若要纳入定期流程,可将其添加到任务调度程序或 CI 工作流中:载入最新名单、执行请求、将 CSV 保存为构建产物,并在 issues 列出现内容时通知团队。不要将密钥打印到日志中;应将其作为运行环境的机密信息保存。
如果经常检查同一批个人资料,可以将 cache_timeout 参数设为缓存秒数。文档指出,IG 默认关闭缓存;设置大于零的值即可在指定时间内启用缓存。只有在允许所选时间间隔内重复使用响应时,才有必要添加此参数。
只检查您有权访问的数据,并仅用于获准的场景。将结果视为 API 特定响应的快照,在自己的日志中记录运行日期,并结合简报定期复核阈值。有关路由、可用字段和集成方式的详情,请参阅IG Scraper API页面和端点官方文档。