scrapy命令行详解(代码片段)

lxl616 lxl616     2022-11-30     781

关键词:

官方文档:https://doc.scrapy.org/en/latest/

 

技术图片

 技术图片

Global commands:

Project-only commands:   在项目目录下才可以执行

 

 

 

startproject

  • Syntax: scrapy startproject <project_name> [project_dir]
  • Requires project: no

Creates a new Scrapy project named project_name, under the project_dir directory. If project_dir wasn’t specified, project_dirwill be the same as project_name.

Usage example:

$ scrapy startproject myproject

genspider

  • Syntax: scrapy genspider [-t template] <name> <domain>
  • Requires project: no

Create a new spider in the current folder or in the current project’s spiders folder, if called from inside a project. The <name> parameter is set as the spider’s name, while <domain> is used to generate the allowed_domains and start_urls spider’s attributes.

Usage example:

$ 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‘

This is just a convenience shortcut command for creating spiders based on pre-defined templates, but certainly not the only way to create spiders. You can just create the spider source code files yourself, instead of using this command.

crawl

  • Syntax: scrapy crawl <spider>
  • Requires project: yes

Start crawling using a spider.

Usage examples:

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

check

  • Syntax: scrapy check [-l] <spider>
  • Requires project: yes

Run contract checks.

Usage examples:

$ 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

list

  • Syntax: scrapy list
  • Requires project: yes

List all available spiders in the current project. The output is one spider per line.

Usage example:

$ scrapy list
spider1
spider2

edit

  • Syntax: scrapy edit <spider>
  • Requires project: yes

Edit the given spider using the editor defined in the EDITORenvironment variable or (if unset) the EDITOR setting.

This command is provided only as a convenience shortcut for the most common case, the developer is of course free to choose any tool or IDE to write and debug spiders.

Usage example:

$ scrapy edit spider1

fetch

  • Syntax: scrapy fetch <url>
  • Requires project: no

Downloads the given URL using the Scrapy downloader and writes the contents to standard output.

The interesting thing about this command is that it fetches the page how the spider would download it. For example, if the spider has a USER_AGENT attribute which overrides the User Agent, it will use that one.

So this command can be used to “see” how your spider would fetch a certain page.

If used outside a project, no particular per-spider behaviour would be applied and it will just use the default Scrapy downloader settings.

Supported options:

  • --spider=SPIDER: bypass spider autodetection and force use of specific spider
  • --headers: print the response’s HTTP headers instead of the response’s body
  • --no-redirect: do not follow HTTP 3xx redirects (default is to follow them)  #有重定向的连接时候使用这个参数

Usage examples:

$ 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)‘]

view

  • Syntax: scrapy view <url>
  • Requires project: no

Opens the given URL in a browser, as your Scrapy spider would “see” it. Sometimes spiders see pages differently from regular users, so this can be used to check what the spider “sees” and confirm it’s what you expect.

Supported options:

  • --spider=SPIDER: bypass spider autodetection and force use of specific spider
  • --no-redirect: do not follow HTTP 3xx redirects (default is to follow them)

Usage example:

$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]

shell

  • Syntax: scrapy shell [url]
  • Requires project: no

Starts the Scrapy shell for the given URL (if given) or empty if no URL is given. Also supports UNIX-style local file paths, either relative with ./ or ../ prefixes or absolute file paths. See Scrapy shell for more info.

Supported options:

  • --spider=SPIDER: bypass spider autodetection and force use of specific spider
  • -c code: evaluate the code in the shell, print the result and exit
  • --no-redirect: do not follow HTTP 3xx redirects (default is to follow them); this only affects the URL you may pass as argument on the command line; once you are inside the shell, fetch(url) will still follow HTTP redirects by default.

Usage example:

$ 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‘)

parse

  • Syntax: scrapy parse <url> [options]
  • Requires project: yes

Fetches the given URL and parses it with the spider that handles it, using the method passed with the --callback option, or parse if not given.

Supported options:

  • --spider=SPIDER: bypass spider autodetection and force use of specific spider
  • --a NAME=VALUE: set spider argument (may be repeated)
  • --callback or -c: spider method to use as callback for parsing the response
  • --meta or -m: additional request meta that will be passed to the callback request. This must be a valid json string. Example: –meta=’“foo” : “bar”’
  • --pipelines: process items through pipelines
  • --rules or -r: use CrawlSpider rules to discover the callback (i.e. spider method) to use for parsing the response
  • --noitems: don’t show scraped items
  • --nolinks: don’t show extracted links
  • --nocolour: avoid using pygments to colorize the output
  • --depth or -d: depth level for which the requests should be followed recursively (default: 1)
  • --verbose or -v: display information for each depth level

Usage example:

$ scrapy parse http://www.example.com/ -c parse_item
[ ... scrapy log lines crawling example.com spider ... ]

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

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

settings

  • Syntax: scrapy settings [options]
  • Requires project: no

Get the value of a Scrapy setting.

If used inside a project it’ll show the project setting value, otherwise it’ll show the default Scrapy value for that setting.

Example usage:

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

技术图片

技术图片

技术图片

 


runspider

  • Syntax: scrapy runspider <spider_file.py>
  • Requires project: no #全局执行

Run a spider self-contained in a Python file, without having to create a project.

Example usage:

$ scrapy runspider myspider.py  
[ ... spider starts crawling ... ]
技术图片

 


version

  • Syntax: scrapy version [-v]
  • Requires project: no

Prints the Scrapy version. If used with -v it also prints Python, Twisted and Platform info, which is useful for bug reports.

技术图片

技术图片

 

bench

New in version 0.17.

  • Syntax: scrapy bench
  • Requires project: no
  • 技术图片

     

Run a quick benchmark test. Benchmarking.

 

 

技术图片

 

 技术图片

 

技术图片

 

python爬虫从入门到成妖之3-----scrapy框架的命令行详解(代码片段)

创建爬虫项目scrapystartproject项目名例子如下:E:crawler>scrapystartprojecttest1NewScrapyproject‘test1‘,usingtemplatedirectory‘d:\python36\lib\site-packages\scrapy\templates\project‘,createdin:E:crawler 查看详情

python爬虫从入门到放弃(十三)之scrapy框架的命令行详解(代码片段)

原文地址https://www.cnblogs.com/zhaof/p/7183220.html这篇文章主要是对的scrapy命令行使用的一个介绍创建爬虫项目scrapystartproject项目名例子如下:localhost:spiderzhaofan$scrapystartprojecttest1NewScrapyproject‘test1‘,usingtemplatedirectory‘/Library/Frameworks/P... 查看详情

scrapy源码解读命令行工具(代码片段)

一、前言scrapy的命令分为全局命令和项目命令。顾名思义,全局命令是不管什么时候都能使用,项目命令只能用于具体已存在的项目上。二、全局命令startproject(创建爬虫项目,一个项目下有一个或多个爬虫--scrapystartprojectproject... 查看详情

scrapy框架的命令行解释(代码片段)

scrapy框架的命令解释 创建爬虫项目scrapystartproject项目名例子如下:scrapystartprojecttest1这个时候爬虫的目录结构就已经创建完成了,目录结构如下:|____scrapy.cfg|____test1||______init__.py||____items.py||____middlewares.py||____pipelines.py||____setti... 查看详情

python命令行参数详解(代码片段)

Python命令行参数详解0.命令行参数1.sys.argv2.getopt2.1getopt.getopt方法2.2Exceptiongetopt.GetoptError3.argparse0.命令行参数通常,对于大型项目程序而言,执行程序的一个必要的步骤是正确处理命令行参数,这些命令行参数是提供给... 查看详情

爬虫scrapy命令行基本用法(代码片段)

1.创建一个新项目:scrapystartprojectmyproject 2.在新项目中创建一个新的spider文件:scrapygenspidermydomainmydomain.commydomain为spider文件名,mydomain.com为爬取网站域名 3.全局命令:startprojectgenspidersettingsrunspidershellfetchviewversion 4.只在... 查看详情

python爬虫——pythonscrapy爬虫框架详解(代码片段)

Scrapy是一个基于Twisted实现的异步处理爬虫框架,该框架使用纯Python语言编写。Scrapy框架应用广泛,常用于数据采集、网络监测,以及自动化测试等。提示:Twisted是一个基于事件驱动的网络引擎框架,同样采用... 查看详情

cypressweb自动化23-cypressrun命令行参数详解(代码片段)

前言非GUI模式下命令行运行cypress,需知道有哪些参数可以使用。查看命令行参数输入-h查看命令行参数cypressrun-hRunsCypresstestsfromtheCLIwithouttheGUIOptions:-b,--browser<browser-name-or-path>runsCypressinthebrowserwiththegivenname.ifafiles 查看详情

pythonscrapy调试模式(代码片段)

scrapy通过命令行创建工程,通过命令行启动爬虫,那么有没有方式可以在IDE中调试我们的爬虫呢?实际上,scrapy是提供给我们工具的,1.首先在工程目录下新建一个脚本文件,作为我们执行爬虫的入口以取代命令行模式。2.编写... 查看详情

《三》kubectl命令行管理工具yaml配置详解(代码片段)

kubectl命令行管理工具kubectl自动补全功能设置执行:1、yuminstall-ybash-completion2、source<(kubectlcompletionbash)3、echo"source<(kubectlcompletionbash)">>~/.bashrc1、创建nginx-test:控制器的名称,默认是deployment控 查看详情

scrapy框架初识(代码片段)

一.scrapy简介   Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍。所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性... 查看详情

scrapy的暂停与重启(代码片段)

首先把爬虫写好后————》在同级文件夹新建一个文件夹(类似于日志)info————》接下来打开命令行cd到这个爬虫————》输入命令:scrapycrawlspider-sJOBDIR=info/001开启ctrl+c暂停注意不能按两次,两次是强制退出了,不是... 查看详情

kubectl命令详解(代码片段)

Kubernetes中文手册Kuberneteskubectl命令表:http://docs.kubernetes.org.cn/683.html一、陈述式管理方法(kubectl命令行管理k8s资源)1.kubernetes集群管理集群资源的唯一入口是通过相应的方法调用apiserver的接口2.kubectl是官方的CLI命令行... 查看详情

爬虫框架windows下scrapy配置环境变量&‘scrapy‘不是内部或外部命令->问题解决(代码片段)

Windows下配置Scrapy环境变量前言原因分析解决办法推荐前言Scrapy框架的安装比较简单:pipinstallscrapy默认会安装Twisted、lxml以及Pywin32、Scrapy等模块。若安装出现报错:可以考虑修改镜像源或者多尝试几次或者再了解原因。由... 查看详情

linuxcurl命令详解(代码片段)

命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具。语法:#curl[option][url]常见参数:-A/-... 查看详情

爬虫框架之scrapy(代码片段)

爬虫框架之Scrapy一、介绍二、安装三、命令行工具四、项目结构以及爬虫应用简介五、Spiders六、Selectors七、Items八、ItemPipelin九、DowloaderMiddeware十、SiderMiddlewear十一、自定义扩展十二、setitings.py十三、获取亚马逊商品信息一、介... 查看详情

k8s读书笔记-kubectl命令行工具用法详解(代码片段)

...为 客户端CLI 工具,可以让用户使用kubectl工具执行命令行,并通过使用k8sAPI与k8s集群的控制面(kube-controller-manager)进行通信。kubectl语法格式kubectl命令行的语法格式如下:kubectl [command] [TYPE] [NAME] [fl 查看详情

scrapy-自定制scrapy命令(代码片段)

在spiders同级创建任意目录,如:commands在其中创建crawlall.py文件(此处文件名就是自定义的命令)fromscrapy.commandsimportScrapyCommandfromscrapy.utils.projectimportget_project_settingsclassCommand(ScrapyCommand):requires_project=Truedef 查看详情