Python 刮擦飞溅-保持记录
我尝试使用scrapy+splash连接网站(感谢) 我知道我已登录,因为我可以在您登录后显示一些可用的元素。但是,当我试图用另一个Python 刮擦飞溅-保持记录,python,session,scrapy,splash-screen,scrapy-splash,Python,Session,Scrapy,Splash Screen,Scrapy Splash,我尝试使用scrapy+splash连接网站(感谢) 我知道我已登录,因为我可以在您登录后显示一些可用的元素。但是,当我试图用另一个请求进入另一个页面时,网站要求再次登录 因此,scrapy(或splash)似乎无法保持会话的活动状态。是否需要启用某些功能以保持日志记录并保持会话处于活动状态 谢谢,Splash从干净状态启动每个渲染,因此如果要保持会话,您需要首先初始化Cookie,并让Scrapy知道渲染过程中设置的Cookie。请参阅scrapy splash自述中的部分。完整的示例可能如下
请求进入另一个页面时,网站要求再次登录
因此,scrapy(或splash)似乎无法保持会话的活动状态。是否需要启用某些功能以保持日志记录并保持会话处于活动状态
谢谢,Splash从干净状态启动每个渲染,因此如果要保持会话,您需要首先初始化Cookie,并让Scrapy知道渲染过程中设置的Cookie。请参阅scrapy splash自述中的部分。完整的示例可能如下所示(从自述文件复制粘贴):
请注意,会话当前需要使用/execute或/run端点,其他端点没有帮助程序
import scrapy
from scrapy_splash import SplashRequest
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
"""
class MySpider(scrapy.Spider):
# ...
yield SplashRequest(url, self.parse_result,
endpoint='execute',
cache_args=['lua_source'],
args={'lua_source': script},
)
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.