スプラッシュを使ってリクエストした後、クッキーにアクセスしようとしています。 以下は、どのようにリクエストを作成したかです。スプラッシュリクエストからクッキーを読む
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'))
これが返されます:私はスプラッシュを使用していない場合は
2017-01-03 12:12:37 [spider] DEBUG: Cookies: None
このコードは動作し、Webページが提供するクッキーを返します。
スプラッシュのドキュメントは一例として、このコードを示しています
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.
私はこれを正しく理解してるかどうかわからないんだけど、私は同じでクッキーにアクセスすることができるはずだと思います私がスプラッシュを使用していないときと同じように。
ミドルウェアの設定:
# 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,
}
だから私の質問は:私はスプラッシュ要求を使用しながら、クッキーにアクセスする方法を教えてください。
リクエストにエンドポイントを追加しましたが、結果はありません。 response.headers.get( 'Set-Cookie')はまだNoneTypeを返します。 responseCookiejarの場合、私はエラーが発生します:AttributeError: 'SplashTextResponse'オブジェクトに 'cookiejar'属性がありません – Casper
@Casper - 記述されているすべてのオプションがsettings.pyに設定されていますか? 'scrap_splash.SplashCookiesMiddleware'は' DOWNLOADER_MIDDLEWARES'に追加されましたか? –
質問をDOWNLOADER_MIDDLEWARES設定変数で更新しました。 – Casper