绕过Midjourney的CSP限制:爬虫请求遭403禁止的解决方案问询
Midjourney 403 Forbidden 绕过方案
你的问题大概率不是CSP限制——CSP是浏览器端的内容加载规则,不会直接导致服务器返回403。更可能是Midjourney升级了反爬机制,对请求真实性做了严格验证。以下是几个可行的调试和绕过方向:
1. 确保Cookie完全有效且实时
你复制的Cookie里包含_dd_s(Datadog会话跟踪)和__Host-next-auth.csrf-token,这类Cookie有效期极短,可能在复制后很快失效。解决方法:
- 重新从浏览器开发者工具(Network标签)复制完整的最新Cookie,包括所有未在代码中列出的项(比如可能存在的
__Secure-next-auth.session-token) - 复制完成后立刻运行脚本,避免Cookie过期
2. 补全缺失的关键请求头
现代网站会检查一系列浏览器专属请求头,你的代码里缺少几个核心项:
- 取消注释
Accept-Encoding,requests支持自动解码gzip/deflate格式 - 添加浏览器客户端提示头:
'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"' - 添加
Referer头:设置为https://www.midjourney.com/,模拟正常跳转逻辑
3. 使用Session维持完整会话
手动设置Cookie容易遗漏会话更新,改用requests.Session()让它自动管理Cookie生命周期:
import requests session = requests.Session() # 先访问主页获取初始会话Cookie session.get('https://www.midjourney.com/', headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', 'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-User': '?1', }) # 再访问目标页面 response = session.get('https://www.midjourney.com/home/?callbackUrl=%2Fapp%2F') print(response.status_code) print(response.text)
4. 检查是否需要JS渲染验证
如果上面的方法都无效,说明Midjourney可能加入了人机验证(比如Cloudflare Turnstile)或需要执行JavaScript生成验证参数。这种情况下,纯HTTP库无法绕过,需要用浏览器自动化工具:
- 使用Playwright/Pyppeteer:模拟真实浏览器环境,执行JS后获取页面内容
- 使用Selenium:配合Chrome/Firefox驱动,模拟用户操作流程
你的原始测试脚本:
import requests cookies = { 'imageSize': 'medium', 'imageLayout_2': 'hover', 'getImageAspect': '2', 'fullWidth': 'false', 'showHoverIcons': 'true', '_dd_s': 'rum=0&expire=1687926962555', '__Host-next-auth.csrf-token': 'c19e3aa92427d9ade40721425bf5affb4955e52f766d0c2a8ca064d3ffef6d9c^%^7Cda294da6acc19312b0399c98e0148de068f41999f76145e15717bdcd3ee9f5c9', '__Secure-next-auth.callback-url': 'https^%^3A^%^2F^%^2Fwww.midjourney.com', } headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', # 'Accept-Encoding': 'gzip, deflate, br', 'Alt-Used': 'www.midjourney.com', 'Connection': 'keep-alive', # 'Cookie': 'imageSize=medium; imageLayout_2=hover; getImageAspect=2; fullWidth=false; showHoverIcons=true; _dd_s=rum=0&expire=1687926962555; __Host-next-auth.csrf-token=c19e3aa92427d9ade40721425bf5affb4955e52f766d0c2a8ca064d3ffef6d9c^%^7Cda294da6acc19312b0399c98e0148de068f41999f76145e15717bdcd3ee9f5c9; __Secure-next-auth.callback-url=https^%^3A^%^2F^%^2Fwww.midjourney.com', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-User': '?1', # Requests doesn't support trailers # 'TE': 'trailers', } response = requests.get('https://www.midjourney.com/home/?callbackUrl=^%^2Fapp^%^2F', cookies=cookies, headers=headers) print(response.status_code) print(response.text)
内容的提问来源于stack exchange,提问作者Math-Milan
相关产品推荐
相关产品推荐

