首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Scrapy Splash中使用Crawlera lua脚本时获得session_id?

如何在Scrapy Splash中使用Crawlera lua脚本时获得session_id?
EN

Stack Overflow用户
提问于 2018-11-27 23:13:00
回答 2查看 568关注 0票数 0

如你所知,当我们尝试在Crawlera中使用Scrapy Splash时,我们使用这个lua脚本:

代码语言:javascript
复制
function use_crawlera(splash)
    -- Make sure you pass your Crawlera API key in the 'crawlera_user' arg.
    -- Have a look at the file spiders/quotes-js.py to see how to do it.
    -- Find your Crawlera credentials in https://app.scrapinghub.com/
    local user = splash.args.crawlera_user

    local host = 'proxy.crawlera.com'
    local port = 8010
    local session_header = 'X-Crawlera-Session'
    local session_id = 'create'

    splash:on_request(function (request)
        request:set_header('X-Crawlera-Cookies', 'disable')
        request:set_header(session_header, session_id)
        request:set_proxy{host, port, username=user, password=''}
    end)

    splash:on_response_headers(function (response)
        if type(response.headers[session_header]) ~= nil then
            session_id = response.headers[session_header]
        end
    end)
end

function main(splash)
    use_crawlera(splash)
        splash:init_cookies(splash.args.cookies)
        assert(splash:go{
            splash.args.url,
            headers=splash.args.headers,
            http_method=splash.args.http_method,
        })    
            assert(splash:wait(3))
        return {
            html = splash:html(),
            cookies = splash:get_cookies(),
        }
end

在这个lua脚本中有一个我非常需要的session_id变量,但是我如何从Scrapy的响应中访问它呢?

我尝试过response.session_idresponse.headers['X-Crawlera-Session'],但两者都不起作用。

EN

回答 2

Stack Overflow用户

发布于 2019-07-05 21:41:13

票数 0
EN

Stack Overflow用户

发布于 2019-07-07 00:59:30

在您的lua脚本中,

  1. 还返回HAR数据(https://splash.readthedocs.io/en/stable/scripting-ref.html#splash-har):

代码语言:javascript
复制
    return {
        html = splash:html(),
        har = splash:har(),
        cookies = splash:get_cookies(),
    }

  1. 假设您正在使用scrapy splash (https://github.com/scrapy-plugins/scrapy-splash),请确保将execute端点设置为您的请求:

meta['splash']['endpoint'] = 'execute'

如果使用scrapy.Request,则render.json是默认端点,但对于scrapy_splash.SplashRequest,默认端点是render.html。查看以下两个示例,了解如何设置端点:https://github.com/scrapy-plugins/scrapy-splash#requests

  1. 现在您可以在您的解析方法中访问X-Crawlera-Session头:

代码语言:javascript
复制
    def parse(self, response):
        headers = json.loads(response.text)['har']['log']['entries'][0]['response']['headers']
        session_id = next(x for x in headers if x['name'] == 'X-Crawlera-Session')['value']
代码语言:javascript
复制
>>> headers = json.loads(response.text)['har']['log']['entries'][0]['response']['headers']
>>> next(x for x in headers if x['name'] == 'X-Crawlera-Session')
{u'name': u'X-Crawlera-Session', u'value': u'2124641382'}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53502649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档