Home

Y's Blog

Salted fish without dreams

Home Notes About Github

2018-10-28
Scrapy命令行详解

原文地址:https://www.cnblogs.com/zhaof/p/7183220.html

这篇文章主要是对的scrapy命令行使用的一个介绍

创建爬虫项目

scrapy startproject 项目名

1
2
3
4
5
6
7
C:\Users\lenovo>scrapy startproject test1
New Scrapy project 'test1', using template directory 'C:\\Users\\lenovo\\Anaconda3\\lib\\site-packages\\scrapy\\templates\\project', created in:
C:\Users\lenovo\test1

You can start your first spider with:
cd test1
scrapy genspider example example.com

这个时候爬虫的目录结构就已经创建完成了,目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
│  scrapy.cfg

└─test1
│ items.py
│ middlewares.py
│ pipelines.py
│ settings.py
│ __init__.py

├─spiders
│ │ __init__.py
│ │
│ └─__pycache__
└─__pycache__

接着我们按照提示可以生成一个spider,这里以百度作为例子,生成spider的命令格式为;

scrapy genspider 爬虫名字 爬虫的网址

1
2
3
C:\Users\lenovo\test1>scrapy genspider baiduSpider baidu.com
Created spider 'baiduSpider' using template 'basic' in module:
test1.spiders.baiduSpider

关于命令详细使用

这里的命令分为全局的命令和项目的命令,全局的命令表示可以在任何地方使用,而项目的命令只能在项目目录下使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
全局的命令有:  
startproject
genspider
settings
runspider
shell
fetch
view
version

项目命令有:
crawl
check
list
edit
parse
bench

genspider

用于生成爬虫,这里scrapy提供给我们不同的几种模板生成spider,默认用的是basic,我们可以通过命令查看所有的模板

1
2
3
4
5
6
C:\Users\lenovo\test1>scrapy genspider -l
Available templates:
basic
crawl
csvfeed
xmlfeed

当我们创建的时候可以指定模板,不指定默认用的basic,如果想要指定模板则通过

scrapy genspider -t 模板名字

1
2
3
C:\Users\lenovo\test1>scrapy genspider -t crawl zhihuspider zhihu.com
Created spider 'zhihuspider' using template 'crawl' in module:
test1.spiders.zhihuspider

crawl

这个是用去启动spider爬虫格式为:

scrapy crawl 爬虫名字

这里需要注意这里的爬虫名字和通过scrapy genspider 生成爬虫的名字是一致的

check

用于检查代码是否有错误,scrapy check

list

scrapy list列出所有可用的爬虫

fetch

scrapy fetch url地址

该命令会通过scrapy downloader 讲网页的源代码下载下来并显示出来

这里有一些参数:

–nolog 不打印日志

–headers 打印响应头信息

–no-redirect 不做跳转

view

scrapy view url地址

该命令会讲网页document内容下载下来,并且在浏览器显示出来

shell

这是一个命令行交互模式

通过scrapy shell url地址进入交互模式

这里我么可以通过css选择器以及xpath选择器获取我们想要的内容,例如我们通过
scrapy shell http://ipython.org/pyreadline.html

这里最后给我们返回一个response,这里的response就和我们通requests请求网页获取的数据是相同的。
view(response)会直接在浏览器显示结果

response.text 获取网页的文本

settings

获取当前的配置信息

通过scrapy settings -h可以获取这个命令的所有帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
C:\Users\lenovo\test1>scrapy settings -h
Usage
=====
scrapy settings [options]

Get settings values

Options
=======
--help, -h show this help message and exit
--get=SETTING print raw setting value
--getbool=SETTING print setting value, interpreted as a boolean
--getint=SETTING print setting value, interpreted as an integer
--getfloat=SETTING print setting value, interpreted as a float
--getlist=SETTING print setting value, interpreted as a list

Global Options
--------------
--logfile=FILE log file. if omitted stderr will be used
--loglevel=LEVEL, -L LEVEL
log level (default: DEBUG)
--nolog disable logging completely
--profile=FILE write python cProfile stats to FILE
--pidfile=FILE write process ID to FILE
--set=NAME=VALUE, -s NAME=VALUE
set/override setting (may be repeated)
--pdb enable pdb on failure

runspider

这个和通过crawl启动爬虫不同,这里是scrapy runspider 爬虫文件名称

所有的爬虫文件都是在项目目录下的spiders文件夹中

version

查看版本信息,并查看依赖库的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
C:\Users\lenovo\test1>scrapy version -v
Scrapy : 1.5.1
lxml : 4.2.1.0
libxml2 : 2.9.8
cssselect : 1.0.3
parsel : 1.5.0
w3lib : 1.19.0
Twisted : 18.7.0
Python : 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.19
00 64 bit (AMD64)]
pyOpenSSL : 18.0.0 (OpenSSL 1.1.0i 14 Aug 2018)
cryptography : 2.3.1
Platform : Windows-10-10.0.10586-SP0

Y's Blog

scribble

Home Notes About Github