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

如何使用JavaScript提取Instagram账号粉丝数(?__a=1接口已失效)

Instagram ?__a=1 接口失效说明

此前在Instagram个人主页URL末尾拼接?__a=1参数,即可获取JSON格式的主页数据,轻松提取各类信息。目前该接口已被Meta官方限制访问,调用后仅返回如下响应内容:

for (;;);{"redirect":"https:\/\/www.instagram.com\/pubgm_silvanus\/"}

访问https://www.instagram.com/pubgm_silvanus?__a=1时就会触发上述重定向,无法直接获取结构化的用户数据,属于普遍情况,大量开发者都遇到了相同的接口失效问题。

粉丝数提取方案

不需要依赖已失效的?__a=1接口,以下两种方法都可以稳定提取账号粉丝数:

1. 浏览器端JavaScript直接提取

打开目标账号的普通个人主页(无需拼接特殊参数),等待页面完全加载后,在浏览器开发者工具控制台执行以下代码即可直接拿到粉丝数,示例账号可正确返回结果11:

// 匹配页面内嵌的用户初始数据
const scriptContent = Array.from(document.querySelectorAll('script:not([src])'))
  .find(s => s.textContent.includes('edge_followed_by'))?.textContent;
const followerMatch = scriptContent?.match(/"edge_followed_by":\{"count":(\d+)\}/);
const followerCount = followerMatch ? Number(followerMatch[1]) : '提取失败,请确认页面已完全加载';
console.log('账号粉丝数:', followerCount);

2. 现有Selenium代码修改方案

当前代码的核心问题是依赖了已失效的接口,只需要做少量修改即可正常运行:

  • 移除get_accounts_list函数中给账号地址拼接?__a=1的逻辑,直接使用不带参数的标准个人主页地址
  • 访问主页后增加显式等待,等待用户信息模块加载完成后再获取页面源码,避免拿到未渲染完成的页面
  • 替换原有的字符串分割提取逻辑,使用正则匹配源码中的"edge_followed_by":{"count":字段,容错率更高

修改后的核心数据提取逻辑参考:

# 访问不带参数的主页
driver.get(f"https://www.instagram.com/{account_name}/")
# 等待页面加载
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//section//ul'))
)
page_source = driver.page_source
import re
follower_match = re.search(r'"edge_followed_by":{"count":(\d+)}', page_source)
if follower_match:
    number_of_followers = follower_match.group(1)

注意:如果使用无头模式访问被反爬检测,可添加--disable-blink-features=AutomationControlled参数隐藏Selenium特征,登录操作不要过于频繁,避免触发账号风控。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 21:06:22