2016-09-25 5 views
2

나는 거미를 만들기 위해 스플래쉬 - 스플래시를 사용합니다. 이제 내가 필요한 것은 세션을 유지하는 것입니다. 그래서 scrapy.downloadermiddlewares.cookies.CookiesMiddleware를 사용하고 set-cookie 헤더를 처리합니다. 나는 COOKIES_DEBUG = True로 설정했기 때문에 이것이 set-cookie 헤더를 처리한다는 것을 알고 있으며, 이는 CookeMiddleware가 set-cookie 헤더에 관해서 출력을하게 만든다.scrapy-splash는 자체 헤더를 반환하지만 사이트의 원래 헤더는 반환하지 않습니다.

문제 : 설정 쿠키 인쇄물이 사라지는 그림에 Splash를 추가하면 실제로 응답 헤더로 얻는 것이 { 'Sun, 25 Sep 2016 12:09:55 GMT '],'Content-Type ': ['text/html; charset = utf-8 '],'Server ': ['TwistedWeb/16.1.1 ']} TwistedWeb을 사용하는 스플래시 렌더링 엔진과 관련된 것입니다.

원래 응답 헤더를 제공하기 위해 스플래시를 알리는 지시문이 있습니까?

답변

3

원래 응답 헤더를 얻으려면 Splash Lua script; README :

쿠키, 헤더, 본문 및 값을 수정하는 방법으로 HTML 응답을 얻으려면 Lua 스크립트를 사용하십시오. lua_source 인수 값 스플래쉬 서버에 캐시되며, 각각의 요청 (이 스플래쉬을 필요로 2.1)로 전송되지 않습니다 :

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}, 
      headers={'X-My-Header': 'value'}, 
     ) 

    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. 

scrapy-시작도 쿠키 처리에 대한 built-in helpers 제공한다; 이 예제에서는 readme에 설명 된대로 scrapy-splash가 configured이되는 즉시 활성화됩니다.

관련 문제