阅读 78

爬虫框架:scrapy

一 介绍

    Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛,可用于如数据挖掘、监测和自动化测试等领域,也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。

    Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架。因此Scrapy使用了一种非阻塞(又名异步)的代码来实现并发。整体架构大致如下

The data flow in Scrapy is controlled by the execution engine, and goes like this:

  1. The Engine gets the initial Requests to crawl from the Spider.

  2. The Engine schedules the Requests in the Scheduler and asks for the next Requests to crawl.

  3. The Scheduler returns the next Requests to the Engine.

  4. The Engine sends the Requests to the Downloader, passing through the Downloader Middlewares (see process_request()).

  5. 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()).

  6. 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()).

  7. 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()).

  8. The Engine sends processed items to Item Pipelines, then send processed Requests to the Scheduler and asks for possible next Requests to crawl.

  9. The process repeats (from step 1) until there are no more requests from the Scheduler.

 

Components:

  1. 引擎(EGINE)

    引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。有关详细信息,请参见上面的数据流部分。

  2. 调度器(SCHEDULER)
    用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址

  3. 下载器(DOWLOADER)
    用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的

  4. 爬虫(SPIDERS)
    SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求

  5. 项目管道(ITEM PIPLINES)
    在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作

  6. 下载器中间件(Downloader Middlewares)
    位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,你可用该中间件做以下几件事

    1. process a request just before it is sent to the Downloader (i.e. right before Scrapy sends the request to the website);

    2. change received response before passing it to a spider;

    3. send a new Request instead of passing received response to a spider;

    4. pass response to a spider without fetching a web page;

    5. silently drop some requests.

  7. 爬虫中间件(Spider Middlewares)
    位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)

官网链接:https://docs.scrapy.org/en/latest/topics/architecture.html

二 安装

复制代码

复制代码

#Windows平台
    1、pip3 install wheel #安装后,便支持通过wheel文件安装软件,wheel文件官网:https://www.lfd.uci.edu/~gohlke/pythonlibs
    3、pip3 install lxml    4、pip3 install pyopenssl    5、下载并安装pywin32:https://sourceforge.net/projects/pywin32/files/pywin32/
    6、下载twisted的wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
    7、执行pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl    8、pip3 install scrapy  
#Linux平台
    1、pip3 install scrapy

复制代码

复制代码

三 命令行工具

复制代码

复制代码

#1 查看帮助
    scrapy -h
    scrapy <command> -h#2 有两种命令:其中Project-only必须切到项目文件夹下才能执行,而Global的命令则不需要    Global commands:
        startproject #创建项目
        genspider    #创建爬虫程序
        settings     #如果是在项目目录下,则得到的是该项目的配置
        runspider    #运行一个独立的python文件,不必创建项目
        shell        #scrapy shell url地址  在交互式调试,如选择器规则正确与否
        fetch        #独立于程单纯地爬取一个页面,可以拿到请求头
        view         #下载完毕后直接弹出浏览器,以此可以分辨出哪些数据是ajax请求
        version      #scrapy version 查看scrapy的版本,scrapy version -v查看scrapy依赖库的版本
    Project-only commands:
        crawl        #运行爬虫,必须创建项目才行,确保配置文件中ROBOTSTXT_OBEY = False
        check        #检测项目中有无语法错误
        list         #列出项目中所包含的爬虫名
        edit         #编辑器,一般不用
        parse        #scrapy parse url地址 --callback 回调函数  #以此可以验证我们的回调函数是否正确
        bench        #scrapy bentch压力测试#3 官网链接
    https://docs.scrapy.org/en/latest/topics/commands.html

复制代码

复制代码

 示范用法

四 项目结构以及爬虫应用简介 

复制代码

复制代码

project_name/
   scrapy.cfg
   project_name/       __init__.py
       items.py
       pipelines.py
       settings.py
       spiders/           __init__.py
           爬虫1.py
           爬虫2.py
           爬虫3.py

复制代码

复制代码

文件说明:

  • scrapy.cfg  项目的主配置信息,用来部署scrapy时使用,爬虫相关的配置信息在settings.py文件中。

  • items.py    设置数据存储模板,用于结构化数据,如:Django的Model

  • pipelines    数据处理行为,如:一般结构化的数据持久化

  • settings.py 配置文件,如:递归的层数、并发数,延迟下载等。强调:配置文件的选项必须大写否则视为无效,正确写法USER_AGENT='xxxx'

  • spiders      爬虫目录,如:创建文件,编写爬虫规则

注意:一般创建爬虫文件时,以网站域名命名

 默认只能在cmd中执行爬虫,如果想在pycharm中执行需要做

 关于windows编码

五 Spiders

1、介绍

#1、Spiders是由一系列类(定义了一个网址或一组网址将被爬取)组成,具体包括如何执行爬取任务并且如何从页面中提取结构化的数据。#2、换句话说,Spiders是你为了一个特定的网址或一组网址自定义爬取和解析页面行为的地方

2、Spiders会循环做如下事情

复制代码

复制代码

#1、生成初始的Requests来爬取第一个URLS,并且标识一个回调函数第一个请求定义在start_requests()方法内默认从start_urls列表中获得url地址来生成Request请求,默认的回调函数是parse方法。回调函数在下载完成返回response时自动触发#2、在回调函数中,解析response并且返回值返回值可以4种:
        包含解析数据的字典
        Item对象
        新的Request对象(新的Requests也需要指定一个回调函数)
        或者是可迭代对象(包含Items或Request)#3、在回调函数中解析页面内容通常使用Scrapy自带的Selectors,但很明显你也可以使用Beutifulsoup,lxml或其他你爱用啥用啥。#4、最后,针对返回的Items对象将会被持久化到数据库通过Item Pipeline组件存到数据库:https://docs.scrapy.org/en/latest/topics/item-pipeline.html#topics-item-pipeline)或者导出到不同的文件(通过Feed exports:https://docs.scrapy.org/en/latest/topics/feed-exports.html#topics-feed-exports)

复制代码

复制代码

3、Spiders总共提供了五种类:

#1、scrapy.spiders.Spider #scrapy.Spider等同于scrapy.spiders.Spider#2、scrapy.spiders.CrawlSpider#3、scrapy.spiders.XMLFeedSpider#4、scrapy.spiders.CSVFeedSpider#5、scrapy.spiders.SitemapSpider

4、导入使用

复制代码

复制代码

# -*- coding: utf-8 -*-import scrapyfrom scrapy.spiders import Spider,CrawlSpider,XMLFeedSpider,CSVFeedSpider,SitemapSpiderclass AmazonSpider(scrapy.Spider): #自定义类,继承Spiders提供的基类
    name = 'amazon'
    allowed_domains = ['www.amazon.cn']
    start_urls = ['http://www.amazon.cn/']    
    def parse(self, response):        pass

复制代码

复制代码

5、class scrapy.spiders.Spider

这是最简单的spider类,任何其他的spider类都需要继承它(包含你自己定义的)。

该类不提供任何特殊的功能,它仅提供了一个默认的start_requests方法默认从start_urls中读取url地址发送requests请求,并且默认parse作为回调函数

复制代码

复制代码

class AmazonSpider(scrapy.Spider):
    name = 'amazon' 
    
    allowed_domains = ['www.amazon.cn'] 
    
    start_urls = ['http://www.amazon.cn/']
    
    custom_settings = {        'BOT_NAME' : 'Egon_Spider_Amazon',        'REQUEST_HEADERS' : {          'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',          'Accept-Language': 'en',
        }
    }    
    def parse(self, response):        pass

复制代码

复制代码

 定制scrapy.spider属性与方法详解

 去重规则:去除重复的url

 例子

 参数传递

6、其他通用Spiders:https://docs.scrapy.org/en/latest/topics/spiders.html#generic-spiders

六 Selectors

复制代码

复制代码

#1 //与/#2 text#3、extract与extract_first:从selector对象中解出内容#4、属性:xpath的属性加前缀@#4、嵌套查找#5、设置默认值#4、按照属性查找#5、按照属性模糊查找#6、正则表达式#7、xpath相对路径#8、带变量的xpath

复制代码

复制代码

 View Code

https://docs.scrapy.org/en/latest/topics/selectors.html

七 Items

https://docs.scrapy.org/en/latest/topics/items.html

八 Item Pipeline

 自定义pipeline

 示范

https://docs.scrapy.org/en/latest/topics/item-pipeline.html

九 Dowloader Middeware

复制代码

复制代码

下载中间件的用途    1、在process——request内,自定义下载,不用scrapy的下载    2、对请求进行二次加工,比如
        设置请求头
        设置cookie
        添加代理
            scrapy自带的代理组件:                from scrapy.downloadermiddlewares.httpproxy import HttpProxyMiddleware                from urllib.request import getproxies

复制代码

复制代码

 下载器中间件

 配置代理

十 Spider Middleware

1、爬虫中间件方法介绍

 爬虫中间件

 2、当前爬虫启动时以及初始请求产生时

 View Code

3、process_spider_input返回None时

 View Code

4、process_spider_input抛出异常时

 View Code

5、指定errback

 View Code

十一 自定义扩展

自定义扩展(与django的信号类似)    1、django的信号是django是预留的扩展,信号一旦被触发,相应的功能就会执行    2、scrapy自定义扩展的好处是可以在任意我们想要的位置添加功能,而其他组件中提供的功能只能在规定的位置执行

 View Code

十二 settings.py

 settings.py

来源https://www.cnblogs.com/wws0904/p/15363957.html

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐