2012-08-14 2 views
0

내가 긁어 모으는 웹 사이트에는 쿠키를 설정하고 백엔드에서 j가 활성화되어 있는지 확인하는 javascript가 있습니다. html 코드에서 쿠키를 추출하는 것은 간단하지만 그 설정은 치료에서 문제가되는 것처럼 보입니다. 그래서 내 코드는 다음과 같습니다치료에 끈적 쿠키 설정

from scrapy.contrib.spiders.init import InitSpider 

class TestSpider(InitSpider): 
    ... 
    rules = (Rule(SgmlLinkExtractor(allow=('products/./index\.html',)), callback='parse_page'),) 

    def init_request(self): 
     return Request(url = self.init_url, callback=self.parse_js) 

    def parse_js(self, response): 
     match = re.search('setCookie\(\'(.+?)\',\s*?\'(.+?)\',', response.body, re.M) 
     if match: 
      cookie = match.group(1) 
      value = match.group(2) 
     else: 
      raise BaseException("Did not find the cookie", response.body) 
     return Request(url=self.test_page, callback=self.check_test_page, cookies={cookie:value}) 

    def check_test_page(self, response): 
     if 'Welcome' in response.body: 
      self.initialized() 

    def parse_page(self, response): 
     scraping.... 

나는 내용이 check_test_page에서 사용할 수 있음을 알 수는 쿠키가 완벽하게 작동합니다. 그러나 올바른 쿠키가없는 CrawlSpider에 링크가 표시되지 않으므로 parse_page까지 도달 할 수 없습니다. 스크래핑 세션 기간 동안 쿠키를 설정하는 방법이 있습니까? 아니면 BaseSpider를 사용하고 모든 요청에 ​​쿠키를 수동으로 추가해야합니까?

덜 바람직한 대안은 어떻게 든 스팸 구성 파일을 통해 쿠키를 변경하는 것입니다 (값은 변경되지 않는 것 같습니다). 그게 가능하니?

+0

기본적으로 모든 쿠키가 전달됩니다. http://doc.scrapy.org/en/latest/ faq.html # does-scrapy-manage-cookies-automatically –

+0

은 서버가 설정 한 쿠키입니다. 내가 볼 수있는 한 영구적 인 쿠키를 클라이언트에서 추가하는 방법은 없습니다 (치료). 따로 요청마다 완료해야합니다. – Leo

답변

0

그것은 InitSpider가 BaseSpider 것으로 밝혀졌다. 그래서 1)이 상황에서 CrawlSpider를 사용할 수있는 방법은 없습니다. 2) 끈적 쿠키를 설정할 방법이 없습니다.

1

이전에 InitSpider을 사용하지 않았습니다.

scrapy.contrib.spiders.init.InitSpider의 코드를 보면 내가 참조 :

def initialized(self, response=None): 
    """This method must be set as the callback of your last initialization 
    request. See self.init_request() docstring for more info. 
    """ 
    self._init_complete = True 
    reqs = self._postinit_reqs[:] 
    del self._postinit_reqs 
    return reqs 

def init_request(self): 
    """This function should return one initialization request, with the 
    self.initialized method as callback. When the self.initialized method 
    is called this spider is considered initialized. If you need to perform 
    several requests for initializing your spider, you can do so by using 
    different callbacks. The only requirement is that the final callback 
    (of the last initialization request) must be self.initialized. 

    The default implementation calls self.initialized immediately, and 
    means that no initialization is needed. This method should be 
    overridden only when you need to perform requests to initialize your 
    spider 
    """ 
    return self.initialized() 

당신이 쓴 :

나는 내용이 check_test_page에 사용할 수 있음을 볼 수있다, 완벽하게 작동 쿠키를. 하지만 에 도달하기까지는 CrawlSpider이 없으므로 쿠키가 없어도 링크가 표시되지 않습니다.

self.initialized과 함께 요청을 콜백으로하지 않았기 때문에 parse_page이 호출되지 않았다고 생각합니다.

나는이 일을해야한다고 생각 :

def check_test_page(self, response): 
    if 'Welcome' in response.body: 
     return self.initialized() 
+0

맞습니다. 직접 소스 코드를 살펴 보았습니다. 그러나 InitSpider는 BaseSpider라고 밝혀졌습니다. 그래서 그것은 1)이 상황에서 CrawlSpider를 사용할 수있는 방법이 없다. 2) 끈적 쿠키를 설정할 수있는 방법이 없다. – Leo