python爬虫系列(三)scrapy基本概念

Scrapy项目的默认结构

欲深入研究爬虫,那就先把这个scrapy的基础概念搞懂。下面我们先看下scrapy的基础目录结构

scrapy.cfg
myproject/
    __init__.py
    items.py
    middlewares.py
    pipelines.py
    settings.py
    spiders/
        __init__.py
        spider1.py
        spider2.py
        ...

scrapy.cfg文件所在的目录称为项目根目录。该文件包含定义项目设置的python模块的名称。比如:

[settings]
default = myproject.settings

scrapy指令

打印一些使用帮助和可用命令:

Scrapy X.Y - no active project

Usage:
  scrapy <command> [options] [args]

Available commands:
  crawl         Run a spider
  fetch         Fetch a URL using the Scrapy downloader
[...]

如果您在Scrapy项目中,第一行将打印当前活动的项目。在上面,它是从项目外部运行的。如果从项目内部运行,它将打印出如下内容:

Scrapy X.Y - project: myproject

Usage:
  scrapy <command> [options] [args]

[...]

创建项目

一般我们使用scrapy的第一件事肯定是创建项目

scrapy startproject myproject [project_dir]

这将在project_dir目录下创建一个Scrapy项目。如果project_dir没有指定,project_dir将是相同的myproject。
接下来,进入新项目目录:

cd project_dir

管理项目

例如,要创建一个新蜘蛛:

scrapy genspider mydomain mydomain.com

某些Scrapy命令(如crawl)必须从Scrapy项目内部运行。
还要记住,某些命令在从项目内部运行时可能会略有不同的行为。例如,user_agent如果获取的url与某个特定的spider相关联,则fetch命令将使用spider-overridden(例如覆盖用户代理的属性)。因为该fetch命令旨在用于检查蜘蛛如何下载页面。

查看更多信息

您还在任何地方可以通过运行以获取有关每个命令的更多信息:

scrapy <command> -h

或者

scrapy -h
全局命令:
  • startproject
  • genspider
  • settings
  • runspider
  • shell
  • fetch
  • view
  • version
仅限项目的命令:
  • crawl
  • check
  • list
  • edit
  • parse
  • bench

startproject命令

句法: scrapy startproject <project_name> [project_dir]

project_name在project_dir 目录下创建一个名为的新Scrapy项目。如果project_dir没有指定,project_dir将是相同的project_name。
示例:

$ scrapy startproject myproject

genspider

句法: scrapy genspider [-t template] <name> <domain>

spiders如果从项目内部调用,则在当前文件夹或当前项目的文件夹中创建新的蜘蛛。该参数设置为蜘蛛的name,而用于生成allowed_domains和start_urls蜘蛛的属性。
示例:

$ scrapy genspider -l
Available templates:
  basic
  crawl
  csvfeed
  xmlfeed

$ scrapy genspider example example.com
Created spider 'example' using template 'basic'

$ scrapy genspider -t crawl scrapyorg scrapy.org
Created spider 'scrapyorg' using template 'crawl'

这只是一个方便的快捷方式命令,用于根据预定义的模板创建蜘蛛,但肯定不是创建蜘蛛的唯一方法。您可以自己创建蜘蛛源代码文件,而不是使用此命令。

抓取

句法: scrapy crawl <spider>

使用蜘蛛开始抓取。
示例:


    $ scrapy crawl myspider
[ ... myspider starts crawling ... ]

检查

句法: scrapy check [-l] <spider>

运行规则检查。
示例:

$ scrapy check -l
first_spider
  * parse
  * parse_item
second_spider
  * parse
  * parse_item

$ scrapy check
[FAILED] first_spider:parse_item
>>> 'RetailPricex' field is missing

[FAILED] first_spider:parse
>>> Returned 92 requests, expected 0..4

列表

句法: scrapy list

列出当前项目中的所有可用蜘蛛。输出是每行一个蜘蛛。
示例:

$ scrapy list
spider1
spider2

编辑

句法: scrapy edit <spider>

使用EDITOR环境变量中定义的编辑器或(如果未设置)EDITOR设置编辑给定的蜘蛛。

此命令仅作为最常见情况的便捷快捷方式提供,开发人员当然可以自由选择任何工具或IDE来编写和调试蜘蛛。
示例:

$ scrapy edit spider1

获取

句法: scrapy fetch <url>

使用Scrapy下载程序下载给定的URL,并将内容写入标准输出。

这个命令的有趣之处在于它获取了蜘蛛下载它的页面。例如,如果蜘蛛具有USER_AGENT 覆盖用户代理的属性,则它将使用该属性。

因此,此命令可用于“查看”您的蜘蛛如何获取某个页面。

如果在项目外部使用,则不会应用特定的每蜘蛛行为,它将仅使用默认的Scrapy下载程序设置。

支持的选项:

  • –spider=SPIDER:绕过蜘蛛自动检测并强制使用特定的蜘蛛
  • –headers:打印响应的HTTP标头而不是响应的正文
  • –no-redirect:不要遵循HTTP 3xx重定向(默认是遵循它们)
    示例:
$ scrapy fetch --nolog http://www.example.com/some/page.html
[ ... html content here ... ]

$ scrapy fetch --nolog --headers http://www.example.com/
{'Accept-Ranges': ['bytes'],
 'Age': ['1263   '],
 'Connection': ['close     '],
 'Content-Length': ['596'],
 'Content-Type': ['text/html; charset=UTF-8'],
 'Date': ['Wed, 18 Aug 2010 23:59:46 GMT'],
 'Etag': ['"573c1-254-48c9c87349680"'],
 'Last-Modified': ['Fri, 30 Jul 2010 15:30:18 GMT'],
 'Server': ['Apache/2.2.3 (CentOS)']}

视图

句法: scrapy view <url>

在浏览器中打开给定的URL,因为您的Scrapy蜘蛛会“看到”它。有时蜘蛛会看到不同于普通用户的页面,因此可以用来检查蜘蛛“看到”的内容并确认它是您所期望的。

支持的选项:

  • –spider=SPIDER:绕过蜘蛛自动检测并强制使用特定的蜘蛛
  • –no-redirect:不要遵循HTTP 3xx重定向(默认是遵循它们)
    示例:
$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]

外壳

句法: scrapy shell [url]

为给定的URL启动Scrapy shell(如果给定),如果没有给出URL,则为空。还支持UNIX样式的本地文件路径,相对于 ./或…/前缀或绝对文件路径。有关详细信息,请参阅Scrapy shell。

支持的选项:

  • –spider=SPIDER:绕过蜘蛛自动检测并强制使用特定的蜘蛛
  • -c code:评估shell中的代码,打印结果并退出
  • –no-redirect:不要遵循HTTP 3xx重定向(默认是遵循它们); 这只会影响您在命令行中作为参数传递的URL; 一旦进入shell,fetch(url)默认情况下仍会遵循HTTP重定向。
    示例:
$ scrapy shell http://www.example.com/some/page.html
[ ... scrapy shell starts ... ]

$ scrapy shell --nolog http://www.example.com/ -c '(response.status, response.url)'
(200, 'http://www.example.com/')

# shell follows HTTP redirects by default
$ scrapy shell --nolog http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F -c '(response.status, response.url)'
(200, 'http://example.com/')

# you can disable this with --no-redirect
# (only for the URL passed as command line argument)
$ scrapy shell --no-redirect --nolog http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F -c '(response.status, response.url)'
(302, 'http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F')

解析

句法: scrapy parse <url> [options]

获取给定的URL并使用处理它的蜘蛛解析它,使用随–callback选项传递的方法,或者parse如果没有给出。

支持的选项:

  • –spider=SPIDER:绕过蜘蛛自动检测并强制使用特定的蜘蛛
  • –a NAME=VALUE:设置蜘蛛参数(可能重复)
  • –callback或-c:用作解析响应的回调的spider方法
  • –meta或-m:将传递给回调请求的其他请求元。这必须是有效的json字符串。示例:-meta =’{“foo”:“bar”}’
  • –pipelines:通过管道处理项目
  • –rules或-r:使用CrawlSpider 规则来发现用于解析响应的回调(即蜘蛛方法)
  • –noitems:不显示刮下的物品
  • –nolinks:不显示提取的链接
  • –nocolour:避免使用pygments为输出着色
  • –depth或-d:递归请求的深度级别(默认值:1)
  • –verbose或-v:显示每个深度级别的信息
    示例:
$ scrapy parse http://www.example.com/ -c parse_item
[ ... scrapy log lines crawling example.com spider ... ]

>>> STATUS DEPTH LEVEL 1 <<<
# Scraped Items  ------------------------------------------------------------
[{'name': u'Example item',
 'category': u'Furniture',
 'length': u'12 cm'}]

# Requests  -----------------------------------------------------------------
[]

设置

句法: scrapy settings [options]

获取Scrapy设置的值。

如果在项目中使用它将显示项目设置值,否则它将显示该设置的默认Scrapy值。
示例:

$ scrapy settings --get BOT_NAME
scrapybot
$ scrapy settings --get DOWNLOAD_DELAY
0

runspider

句法: scrapy runspider <spider_file.py>

在Python文件中运行自包含的蜘蛛,而无需创建项目。
示例:

$ scrapy runspider myspider.py
[ ... spider starts crawling ... ]

猜你喜欢

转载自blog.csdn.net/farley119/article/details/82848821