scrapy中工具的使用
scrapy官方文档 https://scrapy-chs.readthedocs.io/zh_CN/0.24/
scrapy 常用命令
创建项目 scrapy startproject 项目名
创建爬虫 scrapy genspider 文件名 域名
查看可执行爬虫 scrapy list
运行爬虫 scrapy crawl 爬虫名/scrapy runspider 爬虫名.py
进入调试模式 scrapy [-s 变量名=值] shell URL
# 执行创建爬虫命令时 需要进入项目文件夹
# 执行运行爬虫命令时 需要项目文件中存在scrapy.cfg配置文件
# 执行进入调试模式命令时 变量名=值为settings.py文件中的对应配置
scrapy 命令详解
全局命令(若在项目文件中执行会自动加载项目配置)
startproject
genspider
settings
runspider
shell
fetch
view
version
项目命令
crawl
check
list
edit
parse
bench
全局选项
--------------
--logfile=FILE 将日志文件写入到指定文件
--loglevel=LEVEL, -L LEVEL
日志等级 (default: DEBUG)
--nolog 不显示日志
--profile=FILE 将python cProfile stats写入到指定文件
--pidfile=FILE 将进程ID写入指定文件
--set=NAME=VALUE, -s NAME=VALUE
设置/覆盖settings.py中的值(可以重复设置中的值)
--pdb 失败时打印组件日志
—startproject
创建爬虫项目 scrapy startproject 项目名
—genspider
指定爬虫模板创建 scrapy genspider -t 爬虫模板名 爬虫名 目标域名
默认模板创建爬虫 scrapy genspider 爬虫名
列出所有爬虫模板 scrapy genspider -l
—crawl
运行爬虫 scrapy crawl 爬虫名
# 爬虫名为scrapy genspider创建时的爬虫名也可用scrapy list列出所有爬虫名
—check
检查代码是否有误 scrapy check
—list
列出所有可执行爬虫 scrapy list
—edit
编辑爬虫文件 scrapy edit 爬虫名
—fetch
Options
=======
--help, -h 获取帮助信息
--spider=SPIDER 使用指定爬虫
--headers 打印HTTP响应头
--no-redirect 不进行重定向跳转
显示日志 scrapy fetch URL
不显示日志 scrapy fetch URL --nolog
设置日志级别 scrapy fetch URL --loglevel=NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL
不显示日志但保存日志 scrapy fetch URL --logfile=文件名
将预编译保存到指定文件 scrapy fetch URL --profile=文件名
将pid保存到指定文件 scrapy fetch URL --pidfile=文件名
指定的爬虫获取页面 scrapy fetch URL --spider=爬虫名
只输出网页响应头 scrapy fetch URL --headers
禁止重定向 scrapy fetch URL --no-redirect
临时设置setting.py中的值 scrapy fetch URL -s USER_AGENT="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3732.400 QQBrowser/10.5.3819.400"
失败时打印组件日志 scrapy fetch URL --pdb
—settings
Options
=======
--help, -h 获取帮助信息
--get=SETTING 打印变量的值
--getbool=SETTING 打印布尔类型变量的值
--getint=SETTING 打印int类型变量的值
--getfloat=SETTING 打印float类型变量的值
--getlist=SETTING 打印变量的值以列表显示
—runspider
运行爬虫 scrapy runspider spider_name.py
运行爬虫 scrapy crawl spider_name
—version
查看scrapy版本 scrapy version
查看scrapy相关工具信息 scrapy version -v
—shell
直接进入调试模式 scrapy shell
获取网页进入调试模式 scrapy shell URL
scrapy shell
配置文件
scrapy.cfg
作用
1.运行爬虫 scrapy crawl 爬虫名
所需配置文件
2.scrapy shell 样式定义文件
# Automatically created by: scrapy startproject
#
# For more information about the [deploy] section see:
# https://scrapyd.readthedocs.io/en/latest/deploy.html
[settings]
default = hongniangwang_spider.settings
shell = ipython #启用ipython
[deploy]
url = http://localhost:6800/
project = hongniangwang_spider
内置方法
response.status 获取响应状态码
response.headers 获取响应头
response.body 获取响应体
response.text 获取网页源码
xpath(): 传入xpath表达式,返回该表达式所对应的所有节点的selector list列表
extract(): 序列化该节点为Unicode字符串并返回list
css(): 传入CSS表达式,返回该表达式所对应的所有节点的selector list列表,语法同 BeautifulSoup4
re(): 根据传入的正则表达式对数据进行提取,返回Unicode字符串list列表
response.xpath('//a/@href') 返回selectorlist类型
response.xpath('//a/@href').extract() 返回list类型
response.xpath('//a/@href').re('com$') 返回list类型
# xpath,css用来选择目标元素
# re用来过滤已经选择的元素