3.item.py

一、基础知识

1、item.py

  定义存储数据对象,主要衔接spiders(文件夹)和pipelines.py

class ScrapyMusicItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    song_name=scrapy.Field()
    song_album = scrapy.Field()
    song_interval = scrapy.Field()
    song_songmid = scrapy.Field()
    song_singer = scrapy.Field()
    song_url = scrapy.Field()
    pass

 本项目自定义类属性song_name等,代表问题列表中每条问题的内容;

 其中scrapy.Field()是scrapy的特有对象,其主要作用是处理并兼容不同的数据格式,开发者在定义类属性时无须考虑爬取的数据格式,scrapy会对数据格式做相应的处理。

 2、目的

数据抓取的主要目标是从非结构化来源,通常是网页中提取结构化数据,scrapy可以将提取的数据作为Python字典返回,但Python字典缺乏结构,字典的键会在输入是出现拼写的错误或者返回不一致,因此,scrapy提供了Items对象,用于管理和规范爬起数据,使其结构规范化。

Items主要存放在项目文件items.py中,每个Items对象以类的形式声明和命名,类属性为Items的字段,也就是需要存储数据的元数据键。

posted @ 2019-03-24 10:21  凉生暖至  阅读(159)  评论(0编辑  收藏  举报