日志等级

- 日志信息:   使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息;

- 日志信息的种类:

  - ERROR:一般错误;

  - WARNING:警告;

  - INFO:一般的信息;

  - DEBUG: 调试信息;

- 设置日志信息指定输出:

  - 在settings配置文件中添加:

    - LOG_LEVEL = ‘指定日志信息种类’即可。

    - LOG_FILE = 'log.txt'则表示将日志信息写入到指定文件中进行存储。

请求传参

- 在某些情况下,我们爬取的数据不在同一个页面中,例如,我们爬取一个电影网站,电影的名称,评分在一级页面,而要爬取的其他电影详情在其二级子页面中。这时我们就需要用到请求传参。

- 通过 在scrapy.Request()中添加 meta参数 进行传参;

scrapy.Request()

- 案例展示:爬取www.id97.com电影网,将一级页面中的电影名称,类型,评分一级二级页面中的上映时间,导演,片长进行爬取。

  - 爬虫文件

# -*- coding: utf-8 -*-
import scrapy
from moviePro.items import MovieproItem class MovieSpider(scrapy.Spider):
name = 'movie'
allowed_domains = ['www.id97.com']
start_urls = ['http://www.id97.com/'] def parse(self, response):
div_list = response.xpath('//div[@class="col-xs-1-5 movie-item"]') for div in div_list:
item = MovieproItem()
item['name'] = div.xpath('.//h1/a/text()').extract_first()
item['score'] = div.xpath('.//h1/em/text()').extract_first()
#xpath(string(.))表示提取当前节点下所有子节点中的数据值(.)表示当前节点
item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first()
item['detail_url'] = div.xpath('./div/a/@href').extract_first()
#请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传递
yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item}) def parse_detail(self,response):
#通过response获取item
item = response.meta['item']
item['actor'] = response.xpath('//div[@class="row"]//table/tr[1]/a/text()').extract_first()
item['time'] = response.xpath('//div[@class="row"]//table/tr[7]/td[2]/text()').extract_first()
item['long'] = response.xpath('//div[@class="row"]//table/tr[8]/td[2]/text()').extract_first()
#提交item到管道
yield item

  - items文件:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class MovieproItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
score = scrapy.Field()
time = scrapy.Field()
long = scrapy.Field()
actor = scrapy.Field()
kind = scrapy.Field()
detail_url = scrapy.Field()
 

  - 管道文件:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import json
class MovieproPipeline(object):
def __init__(self):
self.fp = open('data.txt','w')
def process_item(self, item, spider):
dic = dict(item)
print(dic)
json.dump(dic,self.fp,ensure_ascii=False)
return item
def close_spider(self,spider):
self.fp.close()

提高scrapy的爬取效率

- 增加并发量:

  - 默认最大的并发量为32,可以通过设置settings文件修改

    CONCURRENT_REQUESTS = 100

    - 将并发改为100

- 降低日志等级:

  - 在运行scrapy时,会有大量日志信息的输出,为了减少CPU的使用率。可以设置log输出信息为INFO或者ERROR即可。修改settings.py

    LOG_LEVEL = 'INFO'

- 禁止cookie:

  - 如果不是真的需要cookie,则在scrapy爬取数据时可以进制cookie从而减少CPU的使用率,提升爬取效率。修改settings.py

    COOKIES_ENABLED = False

- 禁止重试:

  - 对失败的HTTP进行重新请求(重试)会减慢爬取速度,因此可以禁止重试。修改settings.py

    RETRY_ENABLED = False

- 减少下载超时:

  - 如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速被放弃,从而提升效率。修改settings.py

    DOWNLOAD_TIMEOUT = 10 

- 测试案例:

# -*- coding: utf- -*-
import scrapy
from ..items import PicproItem
# 提升spider的爬取效率测试
# 爬取4k高清壁纸网站的图片 class PicSpider(scrapy.Spider):
name = 'pic'
# allowed_domains = ['www.pic.com']
start_urls = ['http://pic.netbian.com/'] def parse(self, response):
li_list = response.xpath('//div[@class="slist"]/ul/li')
print(li_list)
for li in li_list:
img_url ="http://pic.netbian.com/"+li.xpath('./a/span/img/@src').extract_first()
# print(,img_url)
title = li.xpath('./a/span/img/@alt').extract_first()
print("title:", title)
item = PicproItem()
item["name"] = title yield scrapy.Request(url=img_url, callback =self.getImgData,meta={"item":item}) def getImgData(self, response):
item = response.meta['item']
# 取二进制数据在body中
item['img_data'] = response.body yield item
import os
class PicproPipeline(object):
def open_spider(self,spider):
if not os.path.exists('picLib'):
os.mkdir('./picLib')
def process_item(self, item, spider):
imgPath = './picLib/'+item['name']+".jpg"
with open(imgPath,'wb') as fp:
fp.write(item['img_data'])
print(imgPath+'下载成功!')
return item

配置文件:

USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False ITEM_PIPELINES = {
'picPro.pipelines.PicproPipeline': ,
} # 打印具体错误信息
LOG_LEVEL ="ERROR" #提升爬取效率 CONCURRENT_REQUESTS =
COOKIES_ENABLED = False
RETRY_ENABLED = False
DOWNLOAD_TIMEOUT =

Scrapy日志等级以及请求传参的更多相关文章

  1. scrapy框架的日志等级和请求传参, 优化效率

    目录 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 请求传参 如何提高scripy的爬取效率 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 在使 ...

  2. 13.scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是s ...

  3. scrapy框架的日志等级和请求传参

    日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...

  4. scrapy框架之日志等级和请求传参-cookie-代理

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

  5. Scrapy的日志等级和请求传参

    日志等级 日志信息:   使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: 日志信息的种类: ERROR:一般错误: WARNING:警告: INFO:一般的信息: ...

  6. 爬虫开发10.scrapy框架之日志等级和请求传参

    今日概要 日志等级 请求传参 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志 ...

  7. scrapy框架post请求发送,五大核心组件,日志等级,请求传参

    一.post请求发送 - 问题:爬虫文件的代码中,我们从来没有手动的对start_urls列表中存储的起始url进行过请求的发送,但是起始url的确是进行了请求的发送,那这是如何实现的呢? - 解答: ...

  8. Scrapy框架之日志等级和请求传参

    一.Scrapy的日志等级 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. 1.日志等级(信息种类) ERROR:错误 WARN ...

  9. 13,scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy ...

随机推荐

  1. 大型工程多个目录下的Makefile写法

    1.前言 目前从事于linux下程序开发,涉及到多个文件,多个目录,这时候编译文件的任务量比较大,需要写Makefile.关于Makefile的详细内容可以参考网上流传非常广泛的<跟我一起写Ma ...

  2. js遍历对象属性

    对象虽然与数组一样,都是数据的集合. 因为对象中的数据是处于无序状态,不能像数组那样,使用下标来遍历对象的所有属性. 如果要遍历对象属性,就必须要使用for in 语句. var a={ A1=180 ...

  3. ASP.NET MD5加密

    protected void Button1_Click(object sender, EventArgs e) { string pwd = TextBox2.Text.Trim(); Respon ...

  4. 视频及MP3 播放浅析 Jplayer参数详细

    初识jplayer插件是因为它的兼容性是最好的,可以兼容到IE6,官网上对它兼容性有很详细的说明 这个是我选择使用它的首要原因. 现在从需求上来了解它的使用方法吧.第一个需求:MP3格式的音频在网页播 ...

  5. 09 Django组件之用户认证组件

    没有学习Django认证组件之前使用装饰器方法 from django.shortcuts import render, HttpResponse, redirect from app01.MyFor ...

  6. javaee utf-8文件的转换

    package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...

  7. GFS分布式文件系统环境部署与管理

    Gluster分布式文件系统 准备环境五台虚拟机 创建/gfs目录,把软件包全部拷贝目录 把yum仓库的源放进bak下才能执行以下脚本 并指定主机名这四台机器都要执行脚本 [root@localhos ...

  8. 【转载自JHBlogs的博客】postman接口自动化,环境变量的用法详解(附postman常用的方法)

    在实现接口自动测试的时候,会经常遇到接口参数依赖的问题,例如调取登录接口的时候,需要先获取登录的key值,而每次请求返回的key值又是不一样的,那么这种情况下,要实现接口的自动化,就要用到postma ...

  9. nyoj24-素数 距离问题

    素数距离问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数 ...

  10. Problem 48

    Problem 48 The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits of the seri ...