Scrapy框架的命令行详解【转】

Scrapy框架的命令行详解

请给做者点赞 --> 原文连接

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

建立爬虫项目

scrapy startproject 项目名
例子以下:html

localhost:spider zhaofan$ scrapy startproject test1
New Scrapy project 'test1', using template directory '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scrapy/templates/project', created in:
    /Users/zhaofan/Documents/python_project/spider/test1

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

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

|____scrapy.cfg
|____test1
| |______init__.py
| |____items.py
| |____middlewares.py
| |____pipelines.py
| |____settings.py
| |____spiders
| | |______init__.py

接着咱们按照提示能够生成一个spider,这里以百度做为例子,生成spider的命令格式为;
scrapy genspider 爬虫名字 爬虫的网址web

localhost:test1 zhaofan$ scrapy genspider baiduSpider baidu.com
Created spider 'baiduSpider' using template 'basic' in module:
  test1.spiders.baiduSpider
localhost:test1 zhaofan$ 

 

关于命令详细使用

命令的使用范围

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

全局的命令有:

startproject
genspider
settings
runspider
shell
fetch
view
versionshell

项目命令有:

crawl
check
list
edit
parse
bench数据库

startproject

这个命令没什么过多的用法,就是在建立爬虫项目的时候用浏览器

# 建立项目
scrapy startprojects myproject

 

genspider

用于生成爬虫,这里scrapy提供给咱们不一样的几种模板生成spider,默认用的是basic,咱们能够经过命令查看全部的模板框架

# 列出全部的模版
scrapy genspider -l
localhost:test1 zhaofan$ scrapy genspider -l
Available templates:
  basic
  crawl
  csvfeed
  xmlfeed
localhost:test1 zhaofan$ 

 

当咱们建立的时候能够指定模板,不指定默认用的basic,若是想要指定模板则经过
scrapy genspider -t 模板名字scrapy

# 生成一个项目模版
scrapy genspider -t crawl zhihu wwww.zhihu.com
localhost:test1 zhaofan$ scrapy genspider -t crawl zhihuspider zhihu.com
Created spider 'zhihuspider' using template 'crawl' in module:
  test1.spiders.zhihuspider
localhost:test1 zhaofan$ 

 

crawl

这个是用去启动spider爬虫格式为:
scrapy crawl 爬虫名字

# 运行spider
scrapy crawl spidername

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

check

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

# check 用来检查代码是否有错误
scrapy check  

 

list

scrapy list列出全部可用的爬虫

# list 返回项目里面全部spider的名称
scrapy list

 

edit

edit 在命令行下编辑spider ### 不建议运行
scrapy edit myspider

 

fetch

scrapy fetch url地址 
该命令会经过scrapy downloader 讲网页的源代码下载下来并显示出来

这里有一些参数:
--nolog 不打印日志
--headers 打印响应头信息
--no-redirect 不作跳转

# fetch 输出日志及网页源代码
scrapy fetch http://www.baidu.com

# fetch --nolog 只输出源代码
scrapy fetch --nolog http://www.baidu.com

# fetch --nolog --headers 输出响应头
scrapy fetch --nolog --headers http://www.baidu.com

# --nolog --no--redirect 禁止重定向
scrapy fetch --nolog --no--redirect http://www.baidu.com

 

view

scrapy view url地址
该命令会讲网页document内容下载下来,而且在浏览器显示出来

# view 从浏览器中打开网页
scrapy view http://www.taobao.com

由于如今不少网站的数据都是经过ajax请求来加载的,这个时候直接经过requests请求是没法获取咱们想要的数据,因此这个view命令能够帮助咱们很好的判断

shell

这是一个命令行交互模式
经过scrapy shell url地址进入交互模式

# shell 命令行交互模式
csrapy shell http://www.baidu.com

这里我么能够经过css选择器以及xpath选择器获取咱们想要的内容(xpath以及css选择的用法会在下个文章中详细说明),例如咱们经过scrapy shell http://www.baidu.com

这里最后给咱们返回一个response,这里的response就和咱们通requests请求网页获取的数据是相同的。
view(response)会直接在浏览器显示结果
response.text 获取网页的文本
下图是css选择器的一个简单用法

 

settings

获取当前的配置信息
经过scrapy settings -h能够获取这个命令的全部帮助信息

# 获取帮助信息
scrapy settings -h 
localhost:jobboleSpider zhaofan$ 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

拿一个例子进行简单的演示:(这里是个人这个项目的settings配置文件中配置了数据库的相关信息,能够经过这种方式获取,若是没有获取的则为None)

localhost:jobboleSpider zhaofan$ scrapy settings --get=MYSQL_HOST
192.168.1.18
localhost:jobboleSpider zhaofan$ 

 

runspider

这个和经过crawl启动爬虫不一样,这里是scrapy runspider 爬虫文件名称
全部的爬虫文件都是在项目目录下的spiders文件夹中

 

version

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

localhost:~ zhaofan$ scrapy version
Scrapy 1.3.2
localhost:~ zhaofan$ scrapy version -v
Scrapy    : 1.3.2
lxml      : 3.7.3.0
libxml2   : 2.9.4
cssselect : 1.0.1
parsel    : 1.1.0
w3lib     : 1.17.0
Twisted   : 17.1.0
Python    : 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) - [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
pyOpenSSL : 16.2.0 (OpenSSL 1.0.2k  26 Jan 2017)
Platform  : Darwin-16.6.0-x86_64-i386-64bit
相关文章
相关标签/搜索