2017-10-04 5 views
0

私はScrapyを初めて使いました。私は1ブロックで複数のアイテムを返す方法について本当に迷っています。複数の返品を返す

基本的には、テキスト、作成者名、およびその引用に関するタグのネストされたタグを含む見積もりを持つ1つのHTMLタグを取得しています。

ここのコードは1つの見積もりを返します。それだけです。それは、ループを使用して残りの部分を返しません。私は何時間もWebを検索してきましたが、私はそれが得られないと思っています。ここに私のコードは、これまでのところです:

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() 

ここで私はこすりしようとしているページがあります:

Link

答えて

0

これを試してみてください。それはあなたが掻きたいと思ったすべてのデータを提供します。

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

私はすでにこの形でこのクモを作りました。私は降伏の代わりにアイテムを使ってそれを作成しようとしています。にもかかわらずあなたの応答をありがとう! –

0

私も同じ問題の解決策を探していました。そして、ここで私が見つけた解決策です:

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) 

をテキストから引用符を削除するには、あなたは 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)) 

私が役に立ったかどうか教えてください。