查看response的具体内容
- response.url
当前响应的url地址 - response.request.url
当前响应对应的请求的url地址 - response.headers
响应头 - response.body
响应体,也就是html的全部代码,默认为byte类型
这个经常在检索某些表单中的信息时搭配re.findall()使用 - response.request.headers
当前响应的请求头
运行时的一些日志解释
logging的使用
以下的操作都是在settings.py文件里进行,设置的内容直接找块空白添加即可
引入logging时,命令窗口只会显示你让它显示的内容,例如 print()
当你定义了LOG_FILE = “log.log”,所有的日志信息也将被保存在该文件中
当然,你可以继续设置LOG_LEVEL = “WARNING”
即日志信息只显示WARNING等级以上的信息
这里,有多个等级,可以自行百度
以下图片为B站的视频截图,
与中间件有关的参数
官方文档:https://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/spider-middleware.html
- 关键字:爬虫请求网页超过默认次数
在爬虫请求网页超过默认次数(默认值为3)时,该请求就会被直接忽略掉,出现这种情况的原因有很多种,网速,服务器自己的问题,那我们应该怎么做呢?
解:修改默认次数
在settings.py添加如下代码
# 重连次数
RETRY_TIMES = 1000
# 超过分的,定义1000次才忽略
当然你也可以改变下载延迟,减轻服务器的负担
# 重连次数
DOWNLOAD_DELAY = 3
# 这个好像在settings.py中是有的,取消注释即可
settings.py里本来就有的各种参数说明
# -*- coding: utf-8 -*-
# Scrapy settings for example 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 = 'example'
# 爬虫位置
SPIDER_MODULES = ['example.spiders']
# 新建爬虫的位置
NEWSPIDER_MODULE = 'example.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
# 发送请求时的代理
#USER_AGENT = 'example (+http://www.yourdomain.com)'
# Obey robots.txt rules
# 是否遵守网页的爬虫规则
ROBOTSTXT_OBEY = False
# Configure maximum concurrent requests performed by Scrapy (default: 16)
# 最大并发请求
#CONCURRENT_REQUESTS = 32
# 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
# 每个IP的最大请求数
#CONCURRENT_REQUESTS_PER_IP = 16
# Disable cookies (enabled by default)
# 携带cookies???
#COOKIES_ENABLED = False
# Disable Telnet Console (enabled by default)
# 插件
#TELNETCONSOLE_ENABLED = False
# Override the default request headers:
# 默认请求头,这里面不要写'User-Agent',因为上面已经写了,程序会自动调用上面的代理
#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 = {
# 'example.middlewares.ExampleSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# 下载中间件
#DOWNLOADER_MIDDLEWARES = {
# 'example.middlewares.ExampleDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
# 插件
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'example.pipelines.ExamplePipeline': 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
# HTTP 缓存
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'