使用Instaloader调用Instagram接口时遇400 Bad Request(feedback_required)问题求助
解决Instaloader访问Instagram时的"feedback_required" 400错误
问题描述
使用Python的Instaloader模块通过用户名获取Instagram账号资料时,触发400 Bad Request错误,状态为"fail",错误信息为"feedback_required",具体报错发生在访问以下GraphQL接口:
400 Bad Request - "fail" status, message "feedback_required" when accessing https://www.instagram.com/graphql/query?query_hash=2b0673e0dc4580674a88d426fe00ea90&variables=%7B%22shortcode%22%3A%22BjA_hztgz8m%22%7D
实现代码
import instaloader import json # Get instance with a custom user agent (desktop browser) loader = instaloader.Instaloader() # Set the user agent to mimic a desktop client loader.context.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" # Load cookies from json file cookie.json that was exported manually with open('cookie.json') as f: cookies_list = json.load(f) session_data = {} for cookie in cookies_list: session_data[cookie['name']] = cookie['value'] # Load session data from the cookies dictionary username = "your_username" # Replace with your Instagram username loader.load_session(username, session_data) # Use Profile class to access metadata of the account profile = instaloader.Profile.from_username(loader.context, 'kevin') print("Data saved to data.json")
解决方案
- 检查Cookie有效性:手动导出的Cookie可能过期或字段缺失,重新从登录状态的浏览器导出完整Cookie,确认包含
csrftoken、sessionid等核心字段,导出后不要退出浏览器登录。 - 更新Instaloader到最新版:旧版本可能适配不了Instagram的API规则,执行命令更新:
pip install --upgrade instaloader - 控制请求频率:Instagram会拦截高频请求,在代码中添加请求间隔,比如:
import time # 获取资料前添加延迟 time.sleep(3) profile = instaloader.Profile.from_username(loader.context, 'kevin') - 更新User-Agent:使用当前主流浏览器的标识,替换老旧的UA,例如:
loader.context.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" - 改用官方登录方式:放弃手动导入Cookie,使用Instaloader自带的登录方法维护会话:
这种方式会自动处理Cookie更新和会话验证,稳定性更高。# 登录并保存会话 loader.login("your_username", "your_password") loader.save_session_to_file() # 后续直接加载会话 loader.load_session_from_file("your_username")
内容的提问来源于stack exchange,提问作者omar abd el fattah
相关产品推荐
相关产品推荐

