2017-05-15 3 views
0

저는 Scrapy의 초보자입니다. 다음과 같은 크롤러를 작성했지만 parse_item이 구문 분석에서 콜백에 의해 호출되지 않는 이유는 모르겠습니다.스카프 콜백이 작동하지 않습니다.

어떤 도움을 환영합니다. 미리 감사드립니다.

class ManualSpider(Spider): 
    name = "manual" 
    allowed_domains = ["https://www.gumtree.com"] 
    start_urls = ['https://www.gumtree.com/flats-houses/london'] 

    def parse_item(self, response): 
     # Create the loader using the response 
     l = ItemLoader(item=StackItem(), response=response) 

     l.add_xpath('title', '//main/div[2]/header/h1/text()', MapCompose(unicode.strip, unicode.title)) 
     l.add_xpath('price', '//header/span/strong/text()', MapCompose(lambda i: i.replace(',', ''), float), 
        re='[,.0-9]+',) 
     l.add_xpath('description', '//p[@itemprop="description"]' 
            '[1]/text()', Join(), MapCompose(unicode.strip)) 
     l.add_xpath('address', '//*[@itemtype="http://schema.org/' 
           'Place"][1]/text()', MapCompose(unicode.strip)) 
     l.add_xpath('location', '//header/strong/span/text()', MapCompose(unicode.strip)) 
     l.add_xpath('image_urls', '//*[@itemprop="image"][1]/@src', MapCompose(
      lambda i: urljoin(response.url, i))) 

     l.add_value('url', response.url) 
     l.add_value('project', "example") 
     l.add_value('spider', self.name) 
     l.add_value('server', socket.gethostname()) 
     l.add_value('date', datetime.datetime.now()) 

     yield l.load_item() 

    def parse(self, response): 

     # Get the next index URLs and yield Requests 
     next_selector = response.xpath('//*[@class="pagination-next"]//@href') 
     for url in next_selector.extract(): 
      yield Request(urljoin(response.url, url)) 

     # Get item URLs and yield Requests 
     item_selector = response.xpath('//div[@id="srp-results"]//article//@href') 
     for url in item_selector.extract(): 
      if url != "": 
       print(urljoin(response.url, url)) 
       yield Request(urljoin(response.url, url), callback=self.parse_item) 

답변

1

callback=self.parse_item로 변경합니다.

대신에 callback=self.parse_item과 같이 함수의 인스턴스를 제공해야합니다.

합니다 alse "은 https : //"제거 allowed_domains

+0

차이에, 둘은 HTTPS를 제거 – altruistic

+1

시도를 작동하지 않습니다 : // 허용 도메인 –

+0

에 감사를 작동 – altruistic

0

당신이 callback="parse_item에 콜백하는 string을 제공하기 때문에 작동하지 않습니다 callback="parse_item"