• Scrapy基本概念——命令行工具


    一、构建项目的命令行使用

    1、多项目的目录结构

    1. scrapy.cfg
    2. firstproject/
    3. __init__.py
    4. items.py
    5. middlewares.py
    6. pipelines.py
    7. settings.py
    8. spiders/
    9. __init__.py
    10. spider1.py
    11. spider2.py
    12. ...
    13. secondproject/
    14. __init__.py
    15. items.py
    16. middlewares.py
    17. pipelines.py
    18. settings.py
    19. spiders/
    20. __init__.py
    21. spider1.py
    22. spider2.py
    23. ...

    2、多项目的配置(scrapy.cfg)

    1. default = firstproject.settings
    2. project2 = secondproject.settings

    3、多项目的切换

    1. >>> scrapy settings --get BOT_NAME
    2. firstproject
    3. >>> set SCRAPY_PROJECT=project2
    4. >>> scrapy settings --get BOT_NAME
    5. secondproject

    4、创建项目

    scrapy startproject  [project_dir]

    5、打开项目目录

    cd 

    6、创建蜘蛛

    scrapy genspider [-t template]  

    7、查看scrapy和命令行的帮助

    scrapy -h scrapy  -h

    二、全局命令

    1、startproject

    1. 语法: scrapy startproject [project_dir]
    2. 用法:创建项目
    3. 项目依赖: no
    4. 例如:scrapy startproject myproject

    2、genspider

    1. 语法:scrapy genspider [-t template]
    2. 用法:创建蜘蛛
    3. 项目依赖:no
    4. 例如:
    5. >>> scrapy genspider -l
    6. Available templates:
    7. basic
    8. crawl
    9. csvfeed
    10. xmlfeed
    11. >>> scrapy genspider example example.com
    12. Created spider 'example' using template 'basic'
    13. >>> scrapy genspider -t crawl scrapyorg scrapy.org
    14. Created spider 'scrapyorg' using template 'crawl'

    3、settings

    1. 语法:scrapy settings [options]
    2. 用法:获取Scrapy设置的值
    3. 项目依赖:no
    4. 例如:
    5. >>> scrapy settings --get BOT_NAME
    6. scrapybot
    7. >>> scrapy settings --get DOWNLOAD_DELAY
    8. 0

    4、runspider

    1. 语法:scrapy runspider
    2. 用法:不需要创建项目,直接运行一个包含在python文件中的蜘蛛
    3. 项目依赖:no
    4. 例如:
    5. >>> scrapy runspider myspider.py
    6. [ ... spider starts crawling ... ]

    5、shell

    1. 语法:scrapy shell [url]
    2. 项目依赖:no
    3. 用法:根据url启动scrapy shell
    4. 选项:
    5. --spider=SPIDER:绕过Spider自动检测并强制使用特定Spider
    6. -c code:评估shell中的代码,打印结果并退出
    7. --no-redirect:不遵循HTTP 3xx重定向(默认为遵循)
    8. 例如:
    9. >>> scrapy shell http://www.example.com/some/page.html
    10. [ ... scrapy shell starts ... ]
    11. >>> scrapy shell --nolog http://www.example.com/ -c '(response.status, response.url)'
    12. (200, 'http://www.example.com/')
    13. # shell follows HTTP redirects by default
    14. >>> scrapy shell --nolog http://httpbin.org/redirect-to?url=http://example.com/ -c '(response.status, response.url)'
    15. (200, 'http://example.com/')
    16. # you can disable this with --no-redirect
    17. # (only for the URL passed as command line argument)
    18. >>> scrapy shell --no-redirect --nolog http://httpbin.org/redirect-to?url=http://example.com/ -c '(response.status, response.url)'
    19. (302, 'http://httpbin.org/redirect-to?url=http://example.com/')

    6、fetch

    1. 语法:scrapy fetch
    2. 项目依赖:no
    3. 用法:使用蜘蛛的设置获取响应页面并写入标准输出,如项目外使用,则使用scrapy下载器默认的设置
    4. 选项:
    5. --spider=SPIDER:绕过Spider自动检测并强制使用特定Spider
    6. --headers:打印响应的HTTP头而不是响应的正文
    7. --no-redirect:不遵循HTTP 3xx重定向(默认为遵循)
    8. 例如:
    9. >>> scrapy fetch --nolog http://www.example.com/some/page.html
    10. [ ... html content here ... ]
    11. >>> scrapy fetch --nolog --headers http://www.example.com/
    12. {'Accept-Ranges': ['bytes'],
    13. 'Age': ['1263 '],
    14. 'Connection': ['close '],
    15. 'Content-Length': ['596'],
    16. 'Content-Type': ['text/html; charset=UTF-8'],
    17. 'Date': ['Wed, 18 Aug 2010 23:59:46 GMT'],
    18. 'Etag': ['"573c1-254-48c9c87349680"'],
    19. 'Last-Modified': ['Fri, 30 Jul 2010 15:30:18 GMT'],
    20. 'Server': ['Apache/2.2.3 (CentOS)']}

    7、view

    1. 语法:scrapy view
    2. 项目依赖:no
    3. 用法:使用蜘蛛的设置获取响应页面,下载并用浏览器打开
    4. 选项:
    5. --spider=SPIDER:绕过Spider自动检测并强制使用特定Spider
    6. --no-redirect:不遵循HTTP 3xx重定向(默认为遵循)
    7. 例如:
    8. >>> scrapy view http://www.example.com/some/page.html
    9. [ ... browser starts ... ]

    8、version

    1. 语法:scrapy version [-v]
    2. 项目依赖:no
    3. 用法:打印版本。如果使用-v还打印python、twisted和platform的信息

    三、项目命令

    1、crawl

    1. 语法:scrapy crawl
    2. 用法:使用蜘蛛爬取
    3. 项目依赖:yes
    4. 例如:
    5. >>> scrapy crawl myspider
    6. [ ... myspider starts crawling ... ]

    2、check

    1. 语法:scrapy check [-l]
    2. 用法:合约检查
    3. 项目依赖:yes
    4. 例如:
    5. >>> scrapy check -l
    6. first_spider
    7. * parse
    8. * parse_item
    9. second_spider
    10. * parse
    11. * parse_item
    12. >>> scrapy check
    13. [FAILED] first_spider:parse_item
    14. 'RetailPricex' field is missing
    15. [FAILED] first_spider:parse
    16. Returned 92 requests, expected 0..4

    3、list

    1. 语法:scrapy list
    2. 用法:列出当前项目中所有可用蜘蛛
    3. 项目依赖:yes
    4. 例如:
    5. >>> scrapy list
    6. spider1
    7. spider2

    4、edit

    1. 语法:scrapy edit
    2. 用法:在可编辑环境使用编辑器编写蜘蛛代码
    3. 项目依赖:yes
    4. 例如:
    5. >>> scrapy edit spider1

    5、parse

    1. 语法:scrapy parse [options]
    2. 用法:测试解析
    3. 项目依赖:yes
    4. 选项:
    5. --spider=SPIDER :绕过Spider自动检测并强制使用特定Spider
    6. --a NAME=VALUE :set spider参数(可以重复)
    7. --callback 或 -c :用作分析响应的回调的spider方法
    8. --meta 或 -m :将传递给回调请求的附加请求元。这必须是有效的JSON字符串。示例:--meta='“foo”:“bar”'
    9. --cbkwargs :将传递给回调的其他关键字参数。这必须是有效的JSON字符串。示例:--cbkwargs='“foo”:“bar”'
    10. --pipelines :通过管道处理项目
    11. --rules 或 -r 使用 CrawlSpider 发现用于解析响应的回调(即spider方法)的规则
    12. --noitems :不显示爬取的项目
    13. --nolinks :不显示提取的链接
    14. --nocolour :避免使用Pygments对输出着色
    15. --depth 或 -d :应递归执行请求的深度级别(默认值:1
    16. --verbose 或 -v :显示每个深度级别的信息
    17. --output 或 -o :将刮取的项目转储到文件
    18. 例如:
    19. >>> scrapy parse --spider=toscrape-css -c parse -d 2 http://quotes.toscrape.com/

    6、bench

    1. 语法:scrapy bench
    2. 用法:快速基准测试
    3. 项目依赖:no

    更多爬虫知识以及实例源码,可关注微信公众号:angry_it_man 

  • 相关阅读:
    浅析Java设计模式【2.1】——代理
    SSD写放大的优化策略要统一标准了吗?
    numpy.around
    pytorch 多卡分布式训练 调用all_gather_object 出现阻塞等待死锁的问题
    快速平方根倒数计算
    ChatGLM2-6B 部署
    Linux 源码安装Ansible 参考篇
    剑指 Offer II 049. 从根节点到叶节点的路径数字之和
    Three.js中加载和渲染3D Tiles
    关于Thread 类及其基本用法
  • 原文地址:https://blog.csdn.net/xuyuanfan77/article/details/127947164