【问题标题】:Read cookies from Splash request从 Splash 请求中读取 cookie
【发布时间】:2025-12-19 15:20:20
【问题描述】:

我在使用 Splash 发出请求后尝试访问 cookie。 以下是我构建请求的方式。

script = """
function main(splash)
  splash:init_cookies(splash.args.cookies)
  assert(splash:go{
    splash.args.url,
    headers=splash.args.headers,
    http_method=splash.args.http_method,
    body=splash.args.body,
    })
  assert(splash:wait(0.5))

  local entries = splash:history()
  local last_response = entries[#entries].response
  return {
    url = splash:url(),
    headers = last_response.headers,
    http_status = last_response.status,
    cookies = splash:get_cookies(),
    html = splash:html(),
  }
end
"""
req = SplashRequest(
    url,
    self.parse_page,
    args={
        'wait': 0.5,
        'lua_source': script,
        'endpoint': 'execute'
    }
)

该脚本与 Splash 文档完全相同。

所以我正在尝试访问网页上设置的 cookie。当我不使用 Splash 时,下面的代码会按我的预期工作,但在使用 Splash 时不会。

self.logger.debug('Cookies: %s', response.headers.get('Set-Cookie'))

使用 Splash 时返回:

2017-01-03 12:12:37 [蜘蛛] 调试:Cookie:无

当我不使用 Splash 时,此代码有效并返回网页提供的 cookie。

Splash 的文档以该代码为例:

def parse_result(self, response):
    # here response.body contains result HTML;
    # response.headers are filled with headers from last
    # web page loaded to Splash;
    # cookies from all responses and from JavaScript are collected
    # and put into Set-Cookie response header, so that Scrapy
    # can remember them.

我不确定我是否理解正确,但我想说我应该能够以与不使用 Splash 时相同的方式访问 cookie。

中间件设置:

# Download middlewares 
DOWNLOADER_MIDDLEWARES = {
    # Use a random user agent on each request
    'crawling.middlewares.RandomUserAgentDownloaderMiddleware': 400,
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
    'scrapy.downloadermiddlewares.cookies.CookiesMiddleware': 700,
    # Enable crawlera proxy
    'scrapy_crawlera.CrawleraMiddleware': 600,
    # Enable Splash to render javascript
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810, 
}

所以我的问题是:如何在使用 Splash 请求时访问 cookie?

Settings.py

spider.py

【问题讨论】:

    标签: python scrapy scrapy-splash splash-js-render


    【解决方案1】:

    您可以设置SPLASH_COOKIES_DEBUG=True 选项来查看所有正在设置的cookies。当scrapy-splash 配置正确时,当前的cookiejar(合并了所有cookie)以response.cookiejar 的形式提供。

    使用response.headers.get('Set-Header') 并不可靠,因为在重定向的情况下(例如 JS 重定向)可能会有多个响应,并且可以在第一个响应中设置 cookie,而脚本仅返回最后一个响应的标头。

    我不确定这是否是您遇到的问题;该代码不是 Splash 文档的精确副本。这里:

    req = SplashRequest(
        url,
        self.parse_page,
        args={
            'wait': 0.5,
            'lua_source': script
        }
    ) 
    

    您正在向/render.json 端点发送请求;它不执行 Lua 脚本;使用endpoint='execute' 来解决这个问题。

    【讨论】:

    • 我已将端点添加到请求中,但没有结果。 response.headers.get('Set-Cookie') 仍然返回 NoneType。对于 response.cookiejar 我收到一个错误:AttributeError: 'SplashTextResponse' object has no attribute 'cookiejar'
    • @Casper - 你确定所有描述的选项都在你的 settings.py 中设置了吗? scrapy_splash.SplashCookiesMiddleware 是否添加到 DOWNLOADER_MIDDLEWARES 中?
    • 我已经用 DOWNLOADER_MIDDLEWARES 设置变量更新了问题。
    • 问题是 CrawleraMiddleware 不能很好地与 Splash 配合使用。这个中间件请求被处理为scrapy -> crawlera -> splash -> remote website,而它应该是scrapy -> splash -> crawlera -> remote website。要使它们一起工作,您需要调整脚本 - 请参阅 Crawlera+Splash 文档:doc.scrapinghub.com/crawlera.html#using-crawlera-with-splash
    • 我已经在没有 Crawlera 的情况下尝试过(通过 custom_settings = { 'CRAWLERA_ENABLED': False } 并且还通过在 settings.py 中评论中间件),但我仍然遇到同样的错误。我再次查看了配置说明,发现我的设置中没有启动中间件:SPIDER_MIDDLEWARES = { 'scrapy_splash.SplashDeduplicateArgsMiddleware': 100, }。我尝试添加它但出现错误:'scrapy_splash' doesn't define any object named 'SplashDeduplicateArgsMiddleware'
    【解决方案2】:

    您正在尝试从服务器端发送的“静态”标头中获取数据,但页面中的 js 代码也可以生成 cookie。这解释了为什么 splash 使用“splash:get_cookies()”。 要访问响应时“cookies”中的值,您应该使用 lua 脚本返回的表。

    return {
       url = splash:url(),
       headers = last_response.headers,
       http_status = last_response.status,
       cookies = splash:get_cookies(),
       html = splash:html(),
    }
    

    尝试改变这一行

    self.logger.debug('Cookies: %s', response.headers.get('Set-Cookie'))
    

    self.logger.debug('Cookies: %s', response.cookies)
    

    【讨论】: