运行时依赖
安装命令
点击复制技能文档
Instagram 公共个人资料抓取器 使用 Instagram 的内部 API 获取公共 Instagram 个人资料数据。无需登录,无需浏览器,无需 Playwright 开销。返回结构化的个人资料统计数据和最近的帖子数据。
要求 pip install httpx 使用 python instagram_scraper.py <用户名> python instagram_scraper.py google python instagram_scraper.py nike
返回内容 个人资料数据 字段 描述 id Instagram 用户 ID username 用户名 full_name 显示名称 biography 个人简介 external_url 个人资料中的链接 is_verified 蓝色验证标志 is_private 账户私密性 is_business_account 商业个人资料标志 business_category_name 商业类别 followers_count 粉丝数量 following_count 关注数量 posts_count 总帖子数量 profile_pic_url 高清个人资料照片 URL
最近的帖子(最多 12 条) 字段 描述 id 帖子 ID shortcode 帖子简码 url 完整帖子 URL display_url 完整分辨率图像 thumbnail_url 缩略图像 is_video 视频帖子标志 caption 帖子标题 likes_count 点赞数量 comments_count 评论数量 timestamp Unix 时间戳
输出结果 结果保存到 ./storage/instagram/<用户名>.json: { "profile": { "username": "google", "full_name": "Google", "followers_count": 12500000, ... }, "recent_posts": [ { "url": "https://www.instagram.com/p/ABC123/", "likes_count": 45230, "caption": "...", ... } ] }
实现 抓取器使用 Instagram 的内部 web API 端点(i.instagram.com/api/v1/users/web_profile_info/)和标准浏览器 User-Agent 以及公共 Instagram 应用 ID。这是 Instagram 网页客户端在首次加载页面时调用的相同端点。
class InstagramScraper: BASE_URL = "https://i.instagram.com/api/v1" APP_ID = "936619743392459" # 公共 Instagram 网页应用 ID
async def get_profile(self, username: str) -> dict: """获取用户的公共个人资料数据。”” ...
def parse_profile(self, raw_data: dict) -> dict: """从原始个人资料数据中提取关键信息。”” ...
async def get_recent_posts(self, raw_profile: dict, limit: int = 12) -> list: """从个人资料数据中提取最近的帖子.”” ...
错误处理 错误原因 解决方法 ValueError:用户不存在 用户名不存在或拼写错误 验证用户名 PermissionError:Instagram 阻止 IP 速率限制或标记 使用 VPN 或住宅代理 HTTP 401 请求被拒绝 旋转 User-Agent 或等待重试
速率限制 Instagram 的内部 API 不公开速率限制,但激进的抓取会触发 IP 阻塞。在抓取多个账户时,在请求之间添加 2-5 秒的延迟。
限制 仅适用于公共个人资料 返回最多 12 条最近的帖子(Instagram 的默认页面大小) 不返回故事、轮播图元数据(超出时间线网格中显示的内容) 视频查看次数不包含在内部 API 响应中
用例 竞争分析:通过每天运行跟踪关注者增长 影响者审核:在合作之前检查参与率(点赞+评论/关注者) 内容研究:分析哪种帖子格式能带来最高的参与度 品牌监控:跟踪竞争对手的发布频率和内容主题