介绍
Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛,可用于如数据挖掘、监测和自动化测试等领域,也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。
Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架。因此Scrapy使用了一种非阻塞(又名异步)的代码来实现并发。
架构
流程解析
- The Engine gets the initial Requests to crawl from the Spider. (引擎从爬虫获取初始Requests )
- The Engine schedules the Requests in the Scheduler and asks for the next Requests to crawl. (引擎将该Requests 放入调度器中,并请求下一个Requests来爬取 )
- The Scheduler returns the next Requests to the Engine. (调度器将下一个Requests 返回给引擎)
- The Engine sends the Requests to the Downloader, passing through the Downloader Middlewares (see process_request()). (经过中间件,引擎将Requests发送给下载器,)
- Once the page finishes downloading the Downloader generates a Response (with that page) and sends it to the Engine, passing through the Downloader Middlewares (see process_response()). (一旦页面爬取完成,下载器就会生成一个Response,再经过中间件,发送给引擎 )
- The Engine receives the Response from the Downloader and sends it to the Spider for processing, passing through the Spider Middleware (see process_spider_input()). (引擎收到下载器返回的Response 后,经过中间件,发送给爬虫处理)
- The Spider processes the Response and returns scraped items and new Requests (to follow) to the Engine, passing through the Spider Middleware (see process_spider_output()). (爬虫处理Response,经过中间件,返回处理后的items 或新的Requests给引擎)
- The Engine sends processed items to Item Pipelines, then send processed Requests to the Scheduler and asks for possible next Requests to crawl. (引擎将处理后的items发送给项目管道,将Requests 发送给调度器,并请求下一个Requests 来爬取)
- The process repeats (from step 1) until there are no more requests from the Scheduler. (不断重复以上流程,直到调度器中没有requests 为止)
组件说明
引擎(EGINE):
引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。
调度器(SCHEDULER):
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址
下载器(DOWLOADER)
用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的
爬虫(SPIDERS)
SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求
项目管道(ITEM PIPLINES)
在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作
下载器中间件(Downloader Middlewares)
位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,你可用该中间件做以下几件事:
- 在request发往下载器之前对requests进行处理(也就是在爬取网站之前)
- 在response 传递给爬虫之前,修改response
- 不给爬虫发送收到的response, 而是给它发送新的request,
- 不爬取网页,直接给爬虫返回response
- 丢弃一些request
爬虫中间件(Spider Middlewares)
位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)
命令行工具
全局命令
startproject 创建项目
genspider:
scrapy genspider [-t template] <name> <domain>
生成爬虫,-l 查看模板; -t 指定模板,name爬虫名,domain域名settings 查看设置
runspider 运行爬虫(运行一个独立的python文件,不必创建项目)
shell :
scrapy shell [url]
进入交互式命令行,可以方便调试–spider=SPIDER 忽略爬虫自动检测,强制使用指定的爬虫
-c 评估代码,打印结果并退出:
$ scrapy shell --nolog http://www.example.com/ -c '(response.status, response.url)' (200, 'http://www.example.com/')
–no-redirect 拒绝重定向
–nolog 不打印日志
response.status 查看响应码
response.url
response.text; response.body 响应文本;响应二进制
view(response) 打开下载到本地的页面,方便分析页面(比如非静态元素)
fetch 查看爬虫是如何获取页面的,常见选项如下:
- –spider=SPIDER 忽略爬虫自动检测,强制使用指定的爬虫
- –headers 查看响应头信息
- –no-redirect 拒绝重定向
view 同交互式命令中的view
version
项目命令
- crawl :
scrapy crawl <spider>
指定爬虫开始爬取(确保配置文件中ROBOTSTXT_OBEY = False) - check:
scrapy check [-l] <spider>
检查语法错误 - list 爬虫list
- edit 命令行模式编辑爬虫(没啥用)
- parse:
scrapy parse <url> [options]
爬取并用指定的回掉函数解析(可以验证我们的回调函数是否正确)
- –callback 或者 -c 指定回调函数
- bench 测试爬虫性能
项目结构和爬虫应用简介
scrapy startproject tutorial
tutorial/
scrapy.cfg # 项目的主配置信息,用来部署scrapy时使用,爬虫相关的配置信息在settings.py文件中
tutorial/ # 项目模块
__init__.py
items.py # 设置数据存储模板,用于结构化数据,类似Django的Model
pipelines.py # 数据处理行为,如:一般结构化的数据持久化
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
待更新