Scrapy框架基本命令与settings.py设置


Posted in Python onFebruary 06, 2020

本文实例讲述了Scrapy框架基本命令与settings.py设置。分享给大家供大家参考,具体如下:

Scrapy框架基本命令

1.创建爬虫项目

scrapy startproject [项目名称]

2.创建爬虫文件

scrapy genspider +文件名+网址

3.运行(crawl)

scrapy crawl 爬虫名称
# -o output 输出数据到文件
scrapy crawl [爬虫名称] -o zufang.json
scrapy crawl [爬虫名称] -o zufang.csv

4.check检查错误

scrapy check

5.list返回项目所有spider

scrapy list

6.view 存储、打开网页

scrapy view http://www.baidu.com

7.scrapy shell, 进入终端

scrapy shell https://www.baidu.com

8.scrapy runspider

scrapy runspider zufang_spider.py

Scrapy框架: settings.py设置

# -*- coding: utf-8 -*-
# Scrapy settings for maitian project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#   https://doc.scrapy.org/en/latest/topics/settings.html
#   https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#   https://doc.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'maitian'
SPIDER_MODULES = ['maitian.spiders']
NEWSPIDER_MODULE = 'maitian.spiders'
#不能批量设置
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'maitian (+http://www.yourdomain.com)'
#默认遵守robots协议
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
#设置日志文件
LOG_FILE="maitian.log"
#日志等级分为5种:1.DEBUG 2.INFO 3.Warning 4.ERROR 5.CRITICAL
#等级越高 输出的日志越少
# LOG_LEVEL="INFO"
#scrapy设置最大并发数 默认16
# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32
#设置批量延迟请求16 等待3秒再发16 秒
# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
#cookie 不生效 默认是True
# Disable cookies (enabled by default)
#COOKIES_ENABLED = False
#远程
# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False
#加载默认的请求头
# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#  'Accept-Language': 'en',
#}
#爬虫中间件
# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#  'maitian.middlewares.MaitianSpiderMiddleware': 543,
#}
#下载中间件
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#  'maitian.middlewares.MaitianDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#  'scrapy.extensions.telnet.TelnetConsole': None,
#}
#在配置文件 开启管道
#优先级的范围 0--1000;值越小 优先级越高
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
#  'maitian.pipelines.MaitianPipeline': 300,
#}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False
# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

更多相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家基于Scrapy框架的Python程序设计有所帮助。

Python 相关文章推荐
python人人网登录应用实例
Sep 26 Python
Python3控制路由器——使用requests重启极路由.py
May 11 Python
Python实现字典去除重复的方法示例
Jul 31 Python
Django2.1.3 中间件使用详解
Nov 26 Python
解决python 文本过滤和清理问题
Aug 28 Python
python将print输出的信息保留到日志文件中
Sep 27 Python
Django中自定义查询对象的具体使用
Oct 13 Python
基于python及pytorch中乘法的使用详解
Dec 27 Python
Python实现bilibili时间长度查询的示例代码
Jan 14 Python
python使用html2text库实现从HTML转markdown的方法详解
Feb 21 Python
使用keras时input_shape的维度表示问题说明
Jun 29 Python
如何使用scrapy中的ItemLoader提取数据
Sep 30 Python
python opencv圆、椭圆与任意多边形的绘制实例详解
Feb 06 #Python
Python输出指定字符串的方法
Feb 06 #Python
python实现简单飞行棋
Feb 06 #Python
python实现飞行棋游戏
Feb 05 #Python
以SQLite和PySqlite为例来学习Python DB API
Feb 05 #Python
Python操作Sqlite正确实现方法解析
Feb 05 #Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 #Python
You might like
SONY SRF-22W(33W)的电路分析和维修案例
2021/03/02 无线电
我的论坛源代码(三)
2006/10/09 PHP
开源php中文分词系统SCWS安装和使用实例
2014/04/11 PHP
php中操作memcached缓存进行增删改查数据的实现代码
2014/08/15 PHP
php使用curl下载指定大小的文件实例代码
2017/09/30 PHP
ThinkPHP 3.2.3实现加减乘除图片验证码
2018/12/05 PHP
javascript 学习笔记(一)DOM基本操作
2011/04/08 Javascript
用正则表达式替换图片地址img标签
2013/11/22 Javascript
javascript运算符——逻辑运算符全面解析
2016/06/27 Javascript
jQuery stop()用法实例详解
2016/07/28 Javascript
利用node.js制作命令行工具方法教程(一)
2017/06/22 Javascript
JS实现带导航城市列表以及输入搜索功能
2018/01/04 Javascript
微信小程序实现卡片层叠滑动效果
2019/06/21 Javascript
微信小程序调用wx.getImageInfo遇到的坑解决
2020/05/31 Javascript
Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
2015/05/16 Python
Python使用ftplib实现简易FTP客户端的方法
2015/06/03 Python
Python 网页解析HTMLParse的实例详解
2017/08/10 Python
django 多对多表的创建和插入代码实现
2019/09/09 Python
解决python-docx打包之后找不到default.docx的问题
2020/02/13 Python
Pycharm中安装wordcloud等库失败问题及终端通过pip安装的Python库如何添加到Pycharm解释器中(推荐)
2020/05/10 Python
pycharm 关掉syntax检查操作
2020/06/09 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
2020/10/30 Python
浅析css3中matrix函数的使用
2016/06/06 HTML / CSS
Html5 audio标签样式的修改
2016/01/28 HTML / CSS
Anya Hindmarch官网:奢侈设计师手袋及配饰
2018/11/15 全球购物
大学毕业生简单自荐信
2013/11/05 职场文书
学校运动会开幕演讲稿
2014/01/04 职场文书
大学生职业生涯十年规划书范文
2014/09/17 职场文书
2015年十一国庆节演讲稿
2015/03/20 职场文书
时尚女魔头观后感
2015/06/04 职场文书
如何写好一份优秀的工作总结?
2019/06/21 职场文书
经典格言警句:没有热忱,世间便无进步
2019/11/13 职场文书
python自动化之如何利用allure生成测试报告
2021/05/02 Python
Golang Gob编码(gob包的使用详解)
2021/05/07 Golang
前端实现滑动按钮AJAX与后端交互的示例代码
2022/02/24 Javascript
python自动获取微信公众号最新文章的实现代码
2022/07/15 Python