如何从splash获得饼干

2024-09-30 06:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个splash请求中获取cookies,但是我一直得到一个错误。在

以下是我使用的代码:

class P2PEye(scrapy.Spider):
    name = 'p2peyeSpider'
    allowed_domains = ['p2peye.com']
    start_urls = ['https://www.p2peye.com/platform/h9/']

    def start_requests(self):
        script = '''
        function main(splash)
          local url = splash.args.url
          assert(splash:go(url))
          assert(splash:wait(0.5))
          return {
            cookies = splash:get_cookies(),
          }
        end
        '''
        for url in self.start_urls:
            yield SplashRequest(url, callback=self.parse, endpoint='render.html',args={'wait': 1, 'lua_source': script})

    def parse(self, response):
        print(response.request.headers.getlist('Set-Cookie'))
        print(response.cookiejar)

这是我的settings.py

^{pr2}$

response.request.headers.getlist('Set-Cookie')的结果是[], 并且response.cookiejar得到了一个错误:AttributeError: 'SplashTextResponse' object has no attribute 'cookiejar'。 那么我怎样才能在不引起错误的情况下得到cookies呢?在


Tags: selfcomurlresponsedef错误scriptargs
1条回答
网友
1楼 · 发布于 2024-09-30 06:33:33

要访问response.cookiejar,您需要返回SplashJsonResponse

尝试在Lua脚本中返回额外的字段:

script = '''
        function main(splash)
          local url = splash.args.url
          assert(splash:go(url))
          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
        '''

相关问题 更多 >

    热门问题