Items

Item objects are simple containers used to collect the scraped data.They provide a dictionary-like api with a convenient syntax for declaring their available fields.

import scrapy;

class Product(scrapy.Item):

  name=scrapy.Field()

  price=scrapy.Field()

  stock=scrapy.Field()

  last_updated=scrapy.Field(serializer=str)

Extending Items

you can extend Items(to add more fields or to change some metadata for some fields)by declaring a subclass of your original Item.

class DiscountedProduct(Product):

  discount_percent=scrapy.Field(serializer=str)

You can also extend fields metadata by using the previous field metadata and appending more values,or changind existing values.

class SpecificProduct(Product):

  name=scrapy.Field(Product.fields['name'],serializer=my_serializer)

Item Objects

1.class scrapy.item.Item([arg])

Return a new Item optionally initialized from the given argument

The only additional attribute provided by Items is:fields

2.Field objects

class scrapy.item.Field([arg])

The Field class is just an alias to the built-in dict class and doesn't provide any extra functionality or attributes.

_______________________________________________________________________________________________________________________________

Built-in spiders reference

Scrapy comes with some useful generic spiders that you can use,to subclass your spiders from .Their aim is to provide convenient functionality for a few common scraping cases.

class scrapy.spider.Spider

This is the simplest spider,and the one from which every other spider must inherit from.

重要属性:

name

A string which defines the name for this spider.it must be unique.This is the most important spider attribute and it is required.

allowed_domains

An optional list of strings containing domains that this spider is allowed to crawl.Requests for URLs not belonging to the domain names specified in this list won't be followed if offsiteMiddleware is enabled.

start_urls

A list of URLs where the spider will begin to crawl from,when no particular URLs are specified.

start_requests()

This is the method called by Scrapy when the spider is opened for scraping when no particular URLs are specified.If particular URLs are specified,the make_requests_from_url() is used instead to create the Requests.

make_requests_from_url(url)

A method that receives a URL and returns a Request object to scrape.Unless overridden,this method returns Requests with the parse() method as their callback function.

parse(response)

The parse method is in charge of processing the response and returning scraped data .

log(message[,level,component])

Log a message

closed(reason)

called when the spider closes.

class scrapy.contrib.spiders.CrawlSpider

This is the most commonly used spider for crawling regular websites,as it provides a convenient mechanism for following links by defining a set of rules.

除了继承自Spider的属性,CrawlSpider还提供以下属性。

rules

Which is a list of one or more Rule objects.Each Rule defines a certain behaviour for crawling the site.

关于Rule对象:

class scrapy.contrib.spiders.Rule(link_extractor,callback=None,cb_kwargs=None,follow=None,process_links=None,process_request=None)

link_extractor is a Link Extractor object which defines how links will be extracted from each crawled page.

callback is a callable or a string to be called for each link extracted with the specified link_extractor.

注意:when writing crawl spider rules,avoid using parse as callback,since the CrawlSpider uses the parse method itself to implement its logic.

cb_kwargs is a dict containing the keyword arguments to be passed to the callback function.

follow is a boolean which specifies if links should be followed from each response extracted with this rule.If callback is None,follow defaults to true(即继续爬取这个链接),otherwise it default to false.

process_request is a callable or a string which will be called with every request extracted by this rule,and must return a request or None.

------------------------------------------------------------------------------------------------------------------------------------

LinkExtractors are objects whose only purpose is to extract links from web pages(scrapy.http.Response objects).

Scrapy内置的Link Extractors有两个,可以根据需要自己来写。

All availble link extractors classes bundled with scrapy are provided in the scrapy.contrib.linkextractors module.

SgmlLinkExtractor

class scrapy.contrib.linkextractors.sgml.SgmlLinkExtractor(allow,...)

The SgmlLinkExtrator extends the base BaseSgmlLinkExtractor by providing additional filters that you can specify to extract links.

allow(a (a list of)regular expression):a single regular expression that the urls must match in order to be extracted,if not given,it will match all links.

【scrapy】Item及Spider的更多相关文章

  1. scrapy 原理,结构,基本命令,item,spider,selector简述

    原理,结构,基本命令,item,spider,selector简述 原理 (1)结构 (2)运行流程 实操 (1) scrapy命令: 注意先把python安装目录的scripts文件夹添加到环境变量 ...

  2. 爬虫(十六):Scrapy框架(三) Spider Middleware、Item Pipeline

    1. Spider Middleware Spider Middleware是介入到Scrapy的Spider处理机制的钩子框架. 当Downloader生成Response之后,Response会被 ...

  3. python爬虫入门(七)Scrapy框架之Spider类

    Spider类 Spider类定义了如何爬取某个(或某些)网站.包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item). 换句话说,Spider就是您定义爬取的动作 ...

  4. [scrapy]Item Loders

    Items Items就是结构化数据的模块,相当于字典,比如定义一个{"title":"","author":""},i ...

  5. scrapy框架之spider

    爬取流程 Spider类定义如何爬取指定的一个或多个网站,包括是否要跟进网页里的链接和如何提取网页内容中的数据. 爬取的过程是类似以下步骤的循环: 1.通过指定的初始URL初始化Request,并指定 ...

  6. Scrapy框架之Spider模板 转

    一.安装scrapy 首先安装依赖库Twisted pip install (依赖库的路径) 在这个网址http://www.lfd.uci.edu/~gohlke/pythonlibs#twiste ...

  7. 第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制

    第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制 用命令创建自动爬虫文件 创建爬虫文件是根据scrap ...

  8. 二十三 Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制

    用命令创建自动爬虫文件 创建爬虫文件是根据scrapy的母版来创建爬虫文件的 scrapy genspider -l  查看scrapy创建爬虫文件可用的母版 Available templates: ...

  9. scrapy item

    item item定义了爬取的数据的model item的使用类似于dict 定义 在items.py中,继承scrapy.Item类,字段类型scrapy.Field() 实例化:(假设定义了一个名 ...

随机推荐

  1. 1+1/2+1/3+...+1/n为素数的证明

    我们考虑不大于 n的最大的2 的幂 2^k. 令 有 其中 a/b是剩下的所有的项的和,由于乘以了最大的 2的幂,所以剩下的所有项的分母都是奇数,故而 b是奇数.如果 m是整数,那么就会导致等式右边的 ...

  2. Win10本地搭建Apache+PHP运行环境

    微软全新操作系统Windows10在190个国家和地区正式同步上市,正版Windows7.Windows8.1用户均可在一年内免费升级.介绍一下在升级后的Windwos10系统上安装及配置Apache ...

  3. k8s部署测试实例

    查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...

  4. Python基础4 迭代器,生成器,装饰器,Json和pickle 数据序列化

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  5. Redux 和 mobx的区别

    Redux: Redux将数据保存在单一store中,Mobx将数据保存在分散的多个store中 Redux需要手动处理变化后的操作,Mobx使用observable保存数据,数据变化后自动处理响应的 ...

  6. 高德地图api之location定位

    关于定位,分为GPS定位和网络定位.本文将详细描述的浏览器定位,属于网络定位.这是一种通过使用高德JS-API来实现位置定位.城市定位的方法,包含了IP定位,检索等多种网络定位方式.如果您的手机支持G ...

  7. 教你学会Linux/Unix下的vi文本编辑器

    vi编辑器是Unix/Linux系统管理员必须学会使用的编辑器.看了不少关于vi的资料,终于得到这个总结. 首先,记住vi编辑器的两个模式:1.命令模式 2.编辑模式. 在一个UNIX/Linux的s ...

  8. My Friends

    HMQ's blog RMY's blog Shq's blog wjyyy‘s blog

  9. UVM入坑系列笔记(一)

    最近本人在做毕业设计,需要用到UVM搭建验证平台,故在网上查找相关资料,看了一些博客和科普,多少有些收获,记录在这里,以便以后复习查看.以下是本人根据网上学习资料整理的笔记,如果有什么不对的地方欢迎指 ...

  10. More Effective C++ - 章节二 : 操作符(operators)

    5. 对定制的 "类型转换函数" 保持警觉 允许编译器执行隐式类型转换,害处多过好处,不要提供转换函数,除非你确定需要. class foo { foo(int a = 0, in ...