Scraping data without having to explicitly define each field to be scraped

后端 未结 4 859
情深已故
情深已故 2021-02-04 15:06

I want to scrape a page of data (using the Python Scrapy library) without having to define each individual field on the page. Instead I want to dynamically generate fields using

4条回答
  •  旧时难觅i
    2021-02-04 15:26

    This solution works with the exporters (scrapy crawl -t json -o output.json):

    import scrapy
    
    class FlexibleItem(scrapy.Item):
        def __setitem__(self, key, value):
            if key not in self.fields:
                self.fields[key] = scrapy.Field()
            super(FlexibleItem, self).__setitem__(key, value)
    

    EDIT: updated to work with latest Scrapy

提交回复
热议问题