簡體   English   中英

每個項目將抓取的對象導出到一個文件中

[英]Export scrapy objects into one file per item

我正在使用scrapy來獲取某些網頁的內容。 有沒有一種方法可以配置scrapy,以便將每個數據行導出到單獨的文件中?

您可以在Spider中產生項目,以返回要在管道中處理的多個項目。

class SomeSpider(Spider):

  ...

  def parse(self, response):
    # some code to parse the webpage

    for some_line in webpage:
        item = YourItem()
        # parse items

        yield item

這將為一個抓取的頁面返回多個項目。 然后,只需指定管道即可將每個項目寫入單獨的文件。

class SomePipeline(object):

  ...      

  def process_item(self, item, spider):
      with open('file.txt', 'w') as f:

          # format your item into a line here

          f.write(line)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM