scrapy深入之定义item
我们可以在其中预先定义好我们需要爬取哪些字段
所以,需要在items.py中这样设置
定义好之后,我们后来在爬虫中使用的时候,就可以直接使用这个类,而不需要在爬虫中定义一个字典。
注意:当我们定义好items.py之后,我们可以不全部使用items.py中定义的字段,但是使用的字段一定要是全部属于items.py中定义的字段
比如说,我们定义了3个字段,title、location_name、money
我们可以只爬取两个字段,例如title、location_name
甚至可以一个字段都不爬取
但是,
1.我们不能够再爬这三个字段之外的数据。
比如说 update_time、requirement……
2.也不能够随意更改爬虫中的字段的名字。
比如说,我认为title这个名字不够准确,我想要使用position_name这个名字来代替。这都是不可以的,因为我们在items.py中已经将这个字段定义好了。
当然,你也可以选择在爬虫中和items.py中同时修改两个字段,或者是直接关闭items.py这个文件。
具体应该如何使用这个类呢?
1.在items.py中定义好你需要在爬虫中爬取好哪些字段
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
# 我们可以在其中预先定义好我们在爬虫中需要爬取哪些字段
import scrapy
class WuyouItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field()
location_name = scrapy.Field()
money = scrapy.Field()