2017-05-15 3 views
0

Ich bin ein Neuling in Scrapy, schrieb ich einen Crawler wie folgt, aber ich weiß nicht, warum Parse_item nicht von Callback in Parse Def aufgerufen wird.Scarpy Callback funktioniert nicht

alle Hilfen sind willkommen. Danke im Voraus.

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) 

Antwort

1

Es ist nicht, weil Sie eine string in callback="parse_item Rückruf geben nicht funktioniert.

Sie sollten stattdessen eine Instanz der Funktion angeben: callback=self.parse_item.

Alse entfernen "https: //" in der allowed_domains

+0

keinen Unterschied, beide nicht – altruistic

+1

versuchen Sie arbeiten, um die https zu entfernen: // in zulässigen Domänen –

+0

Dank Es funktioniert – altruistic

0

ändern callback="parse_item" zu callback=self.parse_item

Verwandte Themen