pipeline 格式化处理

如果对于想要获取更多的数据处理,则可以利用Scrapy的items将数据格式化,然后统一交由pipelines来处理。我们可以在利用pipeline在爬虫开始时打开数据的链接,子爬虫结束后关闭数据库的链接

使用方法:

    a. 先写pipeline类

        class XXXPipeline(object):

            def process_item(self, item, spider):

                return item      

    b. 写Item类

        class XdbItem(scrapy.Item):

            href = scrapy.Field()

            title = scrapy.Field()        

    c. 配置

        ITEM_PIPELINES = {

            'xdb.pipelines.XdbPipeline': 300,

        }

    d. 爬虫,yield每执行一次,process_item就调用一次。

        yield Item对象

import scrapy


class XdbItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    href = scrapy.Field()
    title = scrapy.Field()
items

 

相关文章:

  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2021-07-02
  • 2021-08-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-08-29
  • 2022-01-23
  • 2022-01-12
  • 2021-05-17
相关资源
相似解决方案