问题描述
在 Scrapy 中使用 Splash 时,标头从 Splash 服务器返回,而不是从网站 Splash 呈现。
response.headers
返回:
{b'Server': [b'TwistedWeb/19.7.0'],b'Date': [b'Sun,11 Jul 2021 07:31:32 GMT'],b'Content-Type': [b'text/html; charset=utf-8']}
Connection: Keep-Alive
Content-Length: 5
Content-Type: text/html
Date: Sun,11 Jul 2021 07:05:49 GMT
Keep-Alive: timeout=5,max=100
Server: Apache
X-Cache: HIT
解决方法
我得到了它:
splash_lua_script = """
function main(splash,args)
assert(splash:go(args.url))
assert(splash:wait(0.5))
local entries = splash:history()
local last_response = entries[#entries].response
return {
html = splash:html(),headers = last_response.headers
}
end
"""
然后使用 Scrapy 将其引用到 response.headers
。