2016-10-03 6 views
3

웹 사이트를 긁는 Scrapy Spider가 있으며 해당 웹 사이트는 토큰을 새로 고침하여 액세스 할 수 있어야합니다. 요청이 완료 될 때까지 기다리십시오 - Python Scrapy

def get_ad(self, response): 
    temp_dict = AppextItem() 
    try: 
     Selector(response).xpath('//div[@class="messagebox"]').extract()[0] 
     print("Captcha found when scraping ID "+ response.meta['id'] + " LINK: "+response.meta['link']) 
     self.p_token = '' 

     return Request(url = url_, callback=self.get_p_token, method = "GET",priority=1, meta = response.meta) 

    except Exception: 
     print("Captcha was not found") 

내가 토큰을 새로 고쳐 self.p_token 보안 문자가 발견 될 때 호출되는

get_p_token에 할당하는 get_p_token 방법을 가지고 있지만, 문제가, 다른 요청은 실행 유지.

Captcha가 발견되면 get_p_token 실행이 완료 될 때까지 다음 요청을 보내지 않기를 바랍니다.

나는 priority=1이지만 도움이되지 않습니다.

HERE is full code of Spider

P.S : 나는 새로운 토큰이 발견 될 때까지 기다린 다음 URL의 나머지 부분을 긁어 할 이유가 그래서

가 실제로 그 토큰이 각 URL에 전달됩니다.

답변

0

이 그것에 대해 계속할 것입니다 방법입니다 당신은 코드를 작동 제공하지 않은

def get_p_token(self, response): 
    # generate token 
    ... 
    yield Request(url = response.url, callback=self.no_captcha, method = "GET",priority=1, meta = response.meta, dont_filter=True) 


def get_ad(self, response): 
    temp_dict = AppextItem() 
    try: 
     Selector(response).xpath('//div[@class="messagebox"]').extract()[0] 
     print("Captcha found when scraping ID "+ response.meta['id'] + " LINK: "+response.meta['link']) 
     self.p_token = '' 

     yield Request(url = url_, callback=self.get_p_token, method = "GET",priority=1, meta = response.meta) 

    except Exception: 
     print("Captcha was not found") 
     yield Request(url = url_, callback=self.no_captcha, method = "GET",priority=1, meta = response.meta) 

따라서이 문제의 데모입니다 ... 여기에 논리는 아주 간단합니다 :

captcha가 발견되면 get_p_token으로 이동하고 토큰을 생성 한 후 이전에 요청한 URL을 요청합니다. captcha가 발견되지 않으면 정상적으로 진행됩니다.

+0

이것은 내가 이미 수행하고있는 작업입니다. 하지만'get_p_token' 메서드 내에서 같은'get_ad' 메서드를 호출합니다 ... 전체 코드 http://pastebin.com/X6Q4ZFp2를 참조하십시오. – Umair

관련 문제