2017-10-04 4 views
0

Ich bin neu in Scrapy und ich bin wirklich nur verloren auf, wie ich mehrere Elemente in einem Block zurückgeben kann.Scrapy Return Mehrere Artikel

Grundsätzlich bekomme ich ein HTML-Tag, das ein Zitat enthält, das verschachtelte Tags von Text, Autorennamen und einige Tags über dieses Zitat enthält.

Der Code hier gibt nur ein Zitat zurück und das war es. Es verwendet die Schleife nicht, um den Rest zurückzugeben. Ich habe stundenlang im Internet gesucht und bin hoffnungslos. Ich verstehe es nicht. Hier ist mein Code so weit:

Spider.py

import scrapy 
from scrapy.loader import ItemLoader 
from first_spider.items import FirstSpiderItem 

class QuotesSpider(scrapy.Spider): 
name = 'quotes' 
allowed_domains = ['quotes.toscrape.com'] 
start_urls = ['http://quotes.toscrape.com/'] 

def parse(self, response): 
    l = ItemLoader(item = FirstSpiderItem(), response=response) 

    quotes = response.xpath("//*[@class='quote']") 

    for quote in quotes: 
     text = quote.xpath(".//span[@class='text']/text()").extract_first() 
     author = quote.xpath(".//small[@class='author']/text()").extract_first() 
     tags = quote.xpath(".//meta[@class='keywords']/@content").extract_first() 

     # removes quotation marks from the text 
     for c in ['“', '”']: 
      if c in text: 
       text = text.replace(c, "") 

     l.add_value('text', text) 
     l.add_value('author', author) 
     l.add_value('tags', tags) 
     return l.load_item() 

    next_page_path = 
    response.xpath(".//li[@class='next']/a/@href").extract_first() 

    next_page_url = response.urljoin(next_page_path) 
    yield scrapy.Request(next_page_url) 

Items.py

import scrapy 

class FirstSpiderItem(scrapy.Item): 

text = scrapy.Field() 
author = scrapy.Field() 
tags = scrapy.Field() 

Hier ist die Seite, die ich zu kratzen bin versucht:

Link

Antwort

0

Probieren Sie es aus. Es gibt Ihnen alle Daten, die Sie abkratzen wollten.

import scrapy 

class QuotesSpider(scrapy.Spider): 

    name = 'quotes' 
    start_urls = ['http://quotes.toscrape.com/'] 

    def parse(self, response): 
     for quote in response.xpath("//*[@class='quote']"): 
      text = quote.xpath(".//span[@class='text']/text()").extract_first() 
      author = quote.xpath(".//small[@class='author']/text()").extract_first() 
      tags = quote.xpath(".//meta[@class='keywords']/@content").extract_first() 
      yield {"Text":text,"Author":author,"Tags":tags} 

     next_page = response.xpath(".//li[@class='next']/a/@href").extract_first() 
     if next_page: 
      next_page_url = response.urljoin(next_page) 
      yield scrapy.Request(next_page_url) 
+0

Ich habe diese Spinne bereits in dieser Form erstellt. Ich versuche es mit Items zu erstellen, anstatt nachzugeben. Vielen Dank für Ihre Antwort! –

0

Ich war auch für das gleiche Problem nach einer Lösung zu suchen. Und hier ist die Lösung, die ich gefunden habe:

def parse(self, response): 
    for selector in response.xpath("//*[@class='quote']"): 
     l = ItemLoader(item=FirstSpiderItem(), selector=selector) 
     l.add_xpath('text', './/span[@class="text"]/text()') 
     l.add_xpath('author', '//small[@class="author"]/text()') 
     l.add_xpath('tags', './/meta[@class="keywords"]/@content') 
     yield l.load_item() 

    next_page = response.xpath(".//li[@class='next']/a/@href").extract_first() 
    if next_page is not None: 
     yield response.follow(next_page, callback=self.parse) 

Um Anführungszeichen aus dem Text zu entfernen, können Sie einen Ausgabe-Prozessor in items.py.

from scrapy.loader.processors import MapCompose 

def replace_quotes(text): 
    for c in ['“', '”']: 
     if c in text: 
      text = text.replace(c, "") 
    return text 

class FirstSpiderItem(scrapy.Item): 
    text = scrapy.Field() 
    author = scrapy.Field() 
    tags = scrapy.Field(output_processor=MapCompose(replace_quotes)) 

Bitte lassen Sie mich wissen, ob es hilfreich war.