• python爬虫之scrapy的使用


    一、Scarpy框架介绍



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

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

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

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

    5、项目管道(ITEM PIPLINES)
    在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作
    下载器中间件(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.

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

    二、Scarpy的安装

        1、pip3 install wheel
    2、pip3 install lxml
    3、pip3 install pyopenssl
    4、pip3 install pypiwin32
    5、安装twisted框架
    下载twisted
    http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
    安装下载好的twisted
    pip3 install 下载目录Twisted-17.9.0-cp36-cp36m-win_amd64.whl

    6、pip3 install scrapy

    三、Scarpy使用

     1 1、进入终端cmd
     2         - scrapy
     3             C:Usersadministortra>scrapy
     4             Scrapy 1.6.0 - no active project
     5 
     6 2、创建scrapy项目
     7       1.创建一个文件夹,专门用于存放scrapy项目
     8           - D:Scrapy_prject
     9       2.cmd终端输入命令
    10           scrapy startproject Spider_Project( 项目名)
    11           - 会在 D:Scrapy_prject文件夹下会生成一个文件
    12             Spider_Project : Scrapy项目文件
    13 
    14       3.创建爬虫程序
    15          cd Spider_Project  # 切换到scrapy项目目录下
    16                        # 爬虫程序名称     目标网站域名
    17          scrapy genspider   baidu     www.baidu.com  # 创建爬虫程序
    18 
    19 3、启动scrapy项目,执行爬虫程序
    20 
    21         # 找到爬虫程序文件进行执行
    22         scrapy runspider只能执行某个 爬虫程序.py
    23             # 切换到爬虫程序执行文件目录下
    24             - cd D:Scrapy_prjectSpider_ProjectSpider_Projectspiders
    25             - scrapy runspider baidu.py
    26 
    27         # 根据爬虫名称找到相应的爬虫程序执行
    28         scrapy crawl 爬虫程序名称
    29             # 切换到项目目录下
    30             - cd D:Scrapy_prjectSpider_Project
    31             - scrapy crawl baidu


  • 相关阅读:
    逻辑学的基本运算
    第一性原理:First principle thinking是什么?
    人类认识的基本技能
    编程的本质:简化+抽象+再现
    区块链
    信号、系统、传递、树
    MVVM
    数据驱动 状态驱动
    事件与状态机 事件驱动编程
    数据一致性举例:登录系统
  • 原文地址:https://www.cnblogs.com/lweiser/p/11066949.html
Copyright © 2020-2023  润新知