2017-05-20 4 views
1

Ich bin ein scrapy Tutorial here folgen. Ich habe, glaube ich, den gleichen Code wie das Tutorial, und doch kratzt mein Scraper nur die erste Seite, gibt dann die folgende Nachricht bezüglich meiner ersten Request an eine andere Seite weiter und endet. Habe ich vielleicht meine zweite yield Aussage an der falschen Stelle?Scrapy Schaber nicht Scraping vorbei 1. Seite

DEBUG: Gefiltert Offsite Anfrage 'newyork.craigslist.org': https://newyork.craigslist.org/search/egr?s=120>

2017-05-20 18:21 : 31 [scrapy.core.engine] INFO: Schließen Spinne (fertig)

Hier ist mein Code:

import scrapy 
from scrapy import Request 


class JobsSpider(scrapy.Spider): 
    name = "jobs" 
    allowed_domains = ["https://newyork.craigslist.org/search/egr"] 
    start_urls = ['https://newyork.craigslist.org/search/egr/'] 

    def parse(self, response): 
     jobs = response.xpath('//p[@class="result-info"]') 

     for job in jobs: 
      title = job.xpath('a/text()').extract_first() 
      address = job.xpath('span[@class="result-meta"]/span[@class="result-hood"]/text()').extract_first("")[2:-1] 
      relative_url = job.xpath('a/@href').extract_first("") 
      absolute_url = response.urljoin(relative_url) 

      yield {'URL': absolute_url, 'Title': title, 'Address': address} 

     # scrape all pages 
     next_page_relative_url = response.xpath('//a[@class="button next"]/@href').extract_first() 
     next_page_absolute_url = response.urljoin(next_page_relative_url) 

     yield Request(next_page_absolute_url, callback=self.parse) 

Antwort

1

Ok, so habe ich es heraus. Ich hatte diese Linie zu ändern:

allowed_domains = ["https://newyork.craigslist.org/search/egr"] 

dazu:

allowed_domains = ["newyork.craigslist.org"] 

und jetzt funktioniert es.