Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrapy - Output to Multiple JSON files

I am pretty new to Scrapy. I am looking into using it to crawl an entire website for links, in which I would output the items into multiple JSON files. So I could then upload them to Amazon Cloud Search for indexing. Is it possible to split the items into multiple files instead of having just one giant file in the end? From what I've read, the Item Exporters can only output to one file per spider. But I am only using one CrawlSpider for this task. It would be nice if I could set a limit to the number of items included in each file, like 500 or 1000.

Here is the code I have set up so far (based off the Dmoz.org used in the tutorial):

dmoz_spider.py

import scrapy

from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from tutorial.items import DmozItem

class DmozSpider(CrawlSpider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/",
    ]

    rules = [Rule(LinkExtractor(), callback='parse_item', follow=True)]

    def parse_item(self, response):
       for sel in response.xpath('//ul/li'):
            item = DmozItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            item['desc'] = sel.xpath('text()').extract()
            yield item

items.py

import scrapy

class DmozItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()
    desc = scrapy.Field()

Thanks for the help.

like image 360
liteshade06 Avatar asked Sep 26 '22 14:09

liteshade06


1 Answers

I don't think built-in feed exporters support writing into multiple files.

One option would be to export into a single file in jsonlines format basically, one JSON object per line which is convenient to pipe and split.

Then, separately, after the crawling is done, you can read the file in the desired chunks and write into separate JSON files.


So I could then upload them to Amazon Cloud Search for indexing.

Note that there is a direct Amazon S3 exporter (not sure it helps, just FYI).

like image 126
alecxe Avatar answered Oct 02 '22 16:10

alecxe