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:
Ich habe diese Spinne bereits in dieser Form erstellt. Ich versuche es mit Items zu erstellen, anstatt nachzugeben. Vielen Dank für Ihre Antwort! –