Scrapy是用纯Python实现一个为了爬取网站数据、提取结构性数据而编写的应用框架,用途非常广泛。

框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非常之方便。

Scrapy 使用了 Twisted异步网络框架来处理网络通讯,可以加快我们的下载速度,不用自己去实现异步框架,并且包含了各种中间件接口,可以灵活的完成各种需求。

一 Scrapy框架流程图

  

(1) 组件描述

  Scrapy Engine(引擎): 负责Spider、ItemPipeline、Downloader、Scheduler中间的通讯,信号、数据传递等。

  Scheduler(调度器): 它负责接受引擎发送过来的Request请求,并按照一定的方式进行整理排列,入队,当引擎需要时,交还给引擎。

  Downloader(下载器):负责下载Scrapy Engine(引擎)发送的所有Requests请求,并将其获取到的Responses交还给Scrapy Engine(引擎),由引擎交给Spider来处理,

  Spider(爬虫):它负责处理所有Responses,从中分析提取数据,获取Item字段需要的数据,并将需要跟进的URL提交给引擎,再次进入Scheduler(调度器),

  Item Pipeline(管道):它负责处理Spider中获取到的Item,并进行进行后期处理(详细分析、过滤、存储等)的地方.

  Downloader Middlewares(下载中间件):你可以当作是一个可以自定义扩展下载功能的组件。

  Spider Middlewares(Spider中间件):你可以理解为是一个可以自定扩展和操作引擎和Spider中间通信的功能组件(比如进入Spider的Responses;和从Spider出去的Requests)

(2) 数据流描述

  1. 引擎打开一个网站(open a domain),找到处理该网站的Spider并向该spider请求第一个要爬取的URL(s)。
  2. 引擎从Spider中获取到第一个要爬取的URL并在调度器(Scheduler)以Request调度。
  3. 引擎向调度器请求下一个要爬取的URL。
  4. 调度器返回下一个要爬取的URL给引擎,引擎将URL通过下载中间件(请求(request)方向)转发给下载器(Downloader)。
  5. 一旦页面下载完毕,下载器生成一个该页面的Response,并将其通过下载中间件(返回(response)方向)发送给引擎。
  6. 引擎从下载器中接收到Response并通过Spider中间件(输入方向)发送给Spider处理。
  7. Spider处理Response并返回爬取到的Item及(跟进的)新的Request给引擎。
  8. 引擎将(Spider返回的)爬取到的Item给Item Pipeline,将(Spider返回的)Request给调度器。
  9. (从第二步)重复直到调度器中没有更多地request,引擎关闭该网站。

二 创建项目及相关组件说明

  Scrapy库的安装、项目创建及简单使用参考之前的博客Python网络爬虫之scrapy(一)下面主要对项目各组件进行说明

(1) 项目目录结构

  D:\scrapy_project>scrapy genspider country example.webscraping.com

  

  item.py:类似Django中的models.py,用于声明数据类型,将来报错数据

  middlewares.py:爬虫中间件,可以对请求和响应进行处理

  pipelines.py:管道,作用是将每一个Item对象进行存储,MySql/MongoDB

  settings.py:对爬虫项目进行配置

  spiders:管理对各爬虫项目,具体的爬虫逻辑在各自的项目爬虫文件中

  country.py:创建的爬虫项目

三 Scrapy重要类说明及部分源码分析

1. Response类

(1)查看Response类的属性

from scrapy.http import Response

for key,value in Response.__dict__.items():
print("{0}:{1}".format(key,value))
__module__:scrapy.http.response
__init__:<function Response.__init__ at 0x00000257D64B1C80>
meta:<property object at 0x00000257D64B2458>
_get_url:<function Response._get_url at 0x00000257D64B40D0>
_set_url:<function Response._set_url at 0x00000257D64B4158>
url:<property object at 0x00000257D64B24A8>
_get_body:<function Response._get_body at 0x00000257D64B4268>
_set_body:<function Response._set_body at 0x00000257D64B42F0>
body:<property object at 0x00000257D64B2728>
__str__:<function Response.__str__ at 0x00000257D64B4400>
__repr__:<function Response.__str__ at 0x00000257D64B4400>
copy:<function Response.copy at 0x00000257D64B4488>
replace:<function Response.replace at 0x00000257D64B4510>
urljoin:<function Response.urljoin at 0x00000257D64B4598>
text:<property object at 0x00000257D64B2778>
css:<function Response.css at 0x00000257D64B46A8>
xpath:<function Response.xpath at 0x00000257D64B4730>
follow:<function Response.follow at 0x00000257D64B47B8>
__dict__:<attribute '__dict__' of 'Response' objects>
__weakref__:<attribute '__weakref__' of 'Response' objects>
__doc__:None

  从上面我们会看到三个重要属性(url、body和text),再查看下Response类源码会发现如下代码

url = property(_get_url, obsolete_setter(_set_url, 'url'))
body = property(_get_body, obsolete_setter(_set_body, 'body')) @property
def text(self):
"""For subclasses of TextResponse, this will return the body
as text (unicode object in Python and str in Python )
"""
raise AttributeError("Response content isn't text")

  url、body、text这就是我们在爬虫分析中需要用到的三个重要属性,都可与通过Response对象获得

  例子: 

import scrapy
from lxml import etree class CountrySpider(scrapy.Spider):
name = 'country'
allowed_domains = ['example.webscraping.com']
start_urls = ['http://example.webscraping.com/places/default/view/Afghanistan-1'] #该函数名不能改变,因为scrapy源码中默认callback函数的函数名就是parse
def parse(self, response):
from bs4 import BeautifulSoup as bs
print(response.url)
soup = bs(response.body)
names = [i.string for i in soup.select('td.w2p_fl')]
values = [j.string for j in soup.select('td.w2p_fw')]
dic = dict(zip(names, values))
print(dic)

2. Spider类

(1)样的方法,线查看Spider类提供的属性

import scrapy

for key,val in scrapy.Spider.__dict__.items():
print("{}:{}".format(key,val))
__module__:scrapy.spiders
__doc__:Base class for scrapy spiders. All spiders must inherit from this
class.
name:None
custom_settings:None
__init__:<function Spider.__init__ at 0x000001E161FFFD90>
logger:<property object at 0x000001E161785D18>
log:<function Spider.log at 0x000001E161FFFEA0>
from_crawler:<classmethod object at 0x000001E16178B208>
set_crawler:<function Spider.set_crawler at 0x000001E161FF8048>
_set_crawler:<function Spider._set_crawler at 0x000001E161FF80D0>
start_requests:<function Spider.start_requests at 0x000001E161FF8158>
make_requests_from_url:<function Spider.make_requests_from_url at 0x000001E161FF81E0>
parse:<function Spider.parse at 0x000001E161FF8268>
update_settings:<classmethod object at 0x000001E16178B240>
handles_request:<classmethod object at 0x000001E16178B278>
close:<staticmethod object at 0x000001E161FF7E80>
__str__:<function Spider.__str__ at 0x000001E161FF8488>
__repr__:<function Spider.__str__ at 0x000001E161FF8488>
__dict__:<attribute '__dict__' of 'Spider' objects>
__weakref__:<attribute '__weakref__' of 'Spider' objects>

(2)接下来对其中几个重要的属性和方法进行说明:

start_requests()

  该 方法会默认读取start_urls属性中定义的网址,为每一个网址生成一个Request请求对象,并返回可迭代对象

make_request_from_url(url)

  该方法会被start_request()调用,该方法负责实现生成Request请求对象

close(reason)

  关闭Spider时,该方法会被调用

log(message[,level,component])

  使用该方法可以实现在Spider中添加log

(3)上面几个函数对应的源码

    def start_requests(self):
cls = self.__class__
if method_is_overridden(cls, Spider, 'make_requests_from_url'):
warnings.warn(
"Spider.make_requests_from_url method is deprecated; it "
"won't be called in future Scrapy releases. Please "
"override Spider.start_requests method instead (see %s.%s)." % (
cls.__module__, cls.__name__
),
)
for url in self.start_urls:
yield self.make_requests_from_url(url)
else:
for url in self.start_urls:
yield Request(url, dont_filter=True) def make_requests_from_url(self, url):
""" This method is deprecated. """
return Request(url, dont_filter=True) def log(self, message, level=logging.DEBUG, **kw):
"""Log the given message at the given log level This helper wraps a log call to the logger within the spider, but you
can use it directly (e.g. Spider.logger.info('msg')) or use any other
Python logger too.
"""
self.logger.log(level, message, **kw)

(4)例子:重写start_request()方法

import scrapy
from lxml import etree class CountrySpider(scrapy.Spider):
name = 'country'
allowed_domains = ['example.webscraping.com']
start_urls = ['http://example.webscraping.com/places/default/view/Afghanistan-1',
"http://example.webscraping.com/places/default/view/Aland-Islands-2"] #重写start_request()方法
def start_requests(self):
for url in self.start_urls:
yield self.make_requests_from_url(url) #该函数名不能改变,因为scrapy源码中默认callback函数的函数名就是parse
def parse(self, response):
from bs4 import BeautifulSoup as bs
print(response.url)
soup = bs(response.body)
names = [i.string for i in soup.select('td.w2p_fl')]
values = [j.string for j in soup.select('td.w2p_fw')]
dic = dict(zip(names, values))
print(dic)

3. pipines的编写 

  在项目被蜘蛛抓取后,它被发送到项目管道,它通过顺序执行的几个组件来处理它。

  每个项目管道组件(有时称为“Item Pipeline”)是一个实现简单方法的Python类。他们接收一个项目并对其执行操作,还决定该项目是否应该继续通过流水线或被丢弃并且不再被处理。

  简单理解就是将item的内容进行处理或保存

class CrawlerPipeline(object):
def process_item(self, item, spider): country_name = item["country_name"]
country_area = item["country_area"]
# 后续处理,可以写进文件
return item

  新手必遇到文件,发现process_item没有被调用,解决方案:

(1)在setting.py中进行配置

ITEM_PIPELINES = {
'crawler.pipelines.CrawlerPipeline':,
}
#后面的数字为0-,决定执行的优先级

(2)在爬虫项目的回调函数中def parse(self, response)中记得返回item

  yield item

若要了解更详细的使用方法,可以参考博客:https://www.jianshu.com/p/b8bd95348ffe

Scrapy框架及组件描述的更多相关文章

  1. python爬虫学习之Scrapy框架的工作原理

    一.Scrapy简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为了 页面抓取 (更确切来说, 网 ...

  2. python爬虫之scrapy框架介绍

    一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等) ...

  3. python高级之scrapy框架

    目录: 爬虫性能原理 scrapy框架解析 一.爬虫性能原理 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. 1.同步执行 impor ...

  4. Scrapy框架的基本组成及功能使用

    1.什么是scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架.框架的本质就是集成各种功能.具有很强通用性的项目模板. 2.安装 Linux:       pip3 in ...

  5. Python爬虫开发【第1篇】【Scrapy框架】

    Scrapy 框架介绍 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架. Srapy框架,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以 ...

  6. Scrapy框架(一)

    Scrapy框架(一) 国内镜像源: 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.c ...

  7. scrapy框架介绍

    一,介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性 ...

  8. Python爬虫从入门到放弃(十二)之 Scrapy框架的架构和原理

    这一篇文章主要是为了对scrapy框架的工作流程以及各个组件功能的介绍 Scrapy目前已经可以很好的在python3上运行Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是 ...

  9. 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案

    Python爬虫总结 总的来说,Python爬虫所做的事情分为两个部分,1:将网页的内容全部抓取下来,2:对抓取到的内容和进行解析,得到我们需要的信息. 目前公认比较好用的爬虫框架为Scrapy,而且 ...

随机推荐

  1. 用来在category里加属性的宏

    众所周知,一般的情况下我们是没办法在category里加属性的. 如果想加,需要用到Associated. @interface NSObject (XYFlyweightTransmit) @pro ...

  2. xcode9打包问题

    xcode9打包找不到icon http://blog.csdn.net/yohunl/article/details/78065656 换用xcode8打包 https://www.cnblogs. ...

  3. idea发布到tomcat缺少jar

    主要是需要自己添加jar到artifact,如果单个添加jar,则在右边栏右键选择 如果是把整个lib添加到toamcat发布,则选择下面的提示:

  4. poj2349 Arctic Network - 最小生成树

    2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to ...

  5. Floyd算法 - 最短路径

    2017-07-27 22:21:04 writer:pprp 该算法的本质是动态规划,形式简单,复杂度高为O(n^3): d[i][j] = max(d[i][k]+d[k][j],d[i][j]) ...

  6. 指定library路径

    1.执行 ?.jar文件: 1.1.“java -jar ?.jar” 1.2.如果 ?.jar里面使用了JNI调用了 ?.dll/?.so 等文件,可能会报错 找不到相关的 库文件,如果这样的话,可 ...

  7. ps切图步骤

    1.复制图层到新建 2.alt + i + r  裁剪 依次按 3.ctrl + alt + shift + s  保存 裁剪图标  复制到图层 , 删除背景,并复制样式 就可以做到 背景透明.

  8. shell read

    #!/bin/bash read -p "Enter your account:" acct #提示用户输入用户名read -s -p "Enter your passw ...

  9. python函数语法学习

    Python函数 定义函数 在Python中,定义一个函数用def语句,一次写出函数名.括号中的参数和冒号:,函数返回使用return语句. def myDef(x): if x >= 0: r ...

  10. lightoj1370欧拉函数/素数筛

    这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...