事实证明这有点棘手。以下是我发现的方法:
使用 LUA 脚本的 Splash 调用,从 Scrapy 调用:
scrpitBusinessUnits = """
function main(splash, args)
splash.request_body_enabled = true
splash.response_body_enabled = true
assert(splash:go(args.url))
assert(splash:wait(18))
splash:runjs('document.getElementById("RESP_INQA_WK_BUSINESS_UNIT$prompt").click();')
assert(splash:wait(20))
return {
har = splash:har(),
}
end
"""
yield SplashRequest(
url=self.start_urls[0],
callback=self.parse,
endpoint='execute',
magic_response=True,
meta={'handle_httpstatus_all': True},
args={'lua_source': scrpitBusinessUnits,'timeout':90,'images':0},
)
此脚本通过返回整个页面加载的HAR文件来工作,关键是设置splash.request_body_enabled = true和splash.response_body_enabled = true来获取HAR文件中的实际响应内容。
HAR 文件只是一个具有不同名称的美化 JSON 对象......所以:
def parse(self, response):
harData = json.loads(response.text)
responseData = harData['har']['log']['entries']
...
# Splash appears to base64 encode large content fields,
# you may have to decode the field to load it properly
bisData = base64.b64decode(bisData['content']['text'])
您可以从那里搜索 JSON 对象以获取确切的嵌入响应。
我真的不认为这是一个非常有效的方法,但它确实有效。