1、知识点

""""
pipelines使用:
1、在spiders里面使用yield生成器
list_li = response.xpath("//div[@class='swiper-wrapper']//li")
#print(list_li)
for li in list_li:
#print(li.extract_first())
item = { }
item["name"] = li.xpath(".//h3/text()").extract_first()
item["content"] = li.xpath(".//p[@class='teacherBrief']/text()").extract_first()
#item["content"] = li.xpath(".//p[@class='teacherIntroduction']/text()").extract_first()
#print(item)
yield item #将数据传递道pipelines 2、在pipelines中打印item
class MyspiderPipeline(object):
"""
#第一个管道,这个process_item方法名是不能改
"""
def process_item(self, item, spider):
item["hello"] = "world"
print(item)
return item class MyspiderPipeline1(object):
"""
#第二个管道
"""
def process_item(self, item, spider):
print(item)
return item 3、在settings文件添加pipelines的支持
ITEM_PIPELINES = {
#执行顺序为从小到大,即先执行300,然后在301
'myspider.pipelines.MyspiderPipeline': 300,
'myspider.pipelines.MyspiderPipeline1': 301,
}
"""

2、spider.py文件中通过

yield  item  #将数据传递道pipelines.py中的item
JulyeduSpider.py文件代码
# -*- coding: utf-8 -*-
import scrapy
import logging logger = logging.getLogger(__name__)
class JulyeduSpider(scrapy.Spider):
name = 'julyedu'
allowed_domains = ['julyedu.com']
start_urls = ['http://julyedu.com/']
#这个parse方法名不能改
def parse(self, response):
"""
爬虫七月在线的导师名单
:param response:
:return:
"""
list_li = response.xpath("//div[@class='swiper-wrapper']//li")
#print(list_li)
item = {}
for li in list_li:
item["name"] = li.xpath(".//h3/text()").extract_first()
item["content"] = li.xpath(".//p[@class='teacherBrief']/text()").extract_first()
#item["content"] = li.xpath(".//p[@class='teacherIntroduction']/text()").extract_first()
#print(item)
#将数据传递道pipelines,yield只接受Request,BaseItem,dict,None四种类型
logger.warning(item) #打印日志
yield item

2、修改pipelines.py文件,对其中的item可以操作

# -*- 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 class MyspiderPipeline(object):
"""
第一个管道,这个process_item方法名是不能改
"""
def process_item(self, item, spider):
"""
针对不同的爬虫的数据处理
:param item:spider 传过来的值
:param spider: 传递过来spider的类
:return:
"""
if spider.name == "julyedu":
#print(item)
return item
else:
return item class MyspiderPipeline1(object):
"""
第二个管道
"""
def process_item(self, item, spider):
#print(item)
return item

3、对settings.py文件添加pipelines配置

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

# Scrapy settings for myspider 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 = 'myspider' SPIDER_MODULES = ['myspider.spiders']
NEWSPIDER_MODULE = 'myspider.spiders' LOG_LEVEL = 'WARNING' #增加log日志
LOG_FILE='./log.log' #将log日志保存到文件中
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'myspider (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = True # 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
#CONCURRENT_REQUESTS_PER_IP = 16 # 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 = {
# 'myspider.middlewares.MyspiderSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'myspider.middlewares.MyspiderDownloaderMiddleware': 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 = {
#执行顺序为从小到大,即先执行300,然后在301
'myspider.pipelines.MyspiderPipeline': 300,
'myspider.pipelines.MyspiderPipeline1': 301,
} # 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之scrapy模块pipelines的更多相关文章

  1. python之scrapy模块scrapy-redis使用

    1.redis的使用,自己可以多学习下,个人也是在学习 https://www.cnblogs.com/ywjfx/p/10262662.html官网可以自己搜索下. 2.下载安装scrapy-red ...

  2. python之scrapy模块下载中间件

    知识点 使用方法: 编写一个Downloader Middlewares和我们编写一个pipeline一样,定义一个类,然后在setting中开启 Downloader Middlewares默认的方 ...

  3. python之scrapy模块logging日志

    1.知识点 """ logging : scrapy: settings中设置LOG_LEVEL="WARNING" settings中设置LOG_F ...

  4. python 安装 Scrapy 模块

    环境的安装总是让人多愁善感,爱恨交叉... 本人安装环境:win7 64 + python2.7 先来几个网站 https://doc.scrapy.org/en/latest/intro/insta ...

  5. Python之Scrapy爬虫框架安装及简单使用

    题记:早已听闻python爬虫框架的大名.近些天学习了下其中的Scrapy爬虫框架,将自己理解的跟大家分享.有表述不当之处,望大神们斧正. 一.初窥Scrapy Scrapy是一个为了爬取网站数据,提 ...

  6. [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍

    前面介绍了很多Selenium基于自动测试的Python爬虫程序,主要利用它的xpath语句,通过分析网页DOM树结构进行爬取内容,同时可以结合Phantomjs模拟浏览器进行鼠标或键盘操作.但是,更 ...

  7. python爬虫scrapy项目详解(关注、持续更新)

    python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...

  8. 第三百二十四节,web爬虫,scrapy模块介绍与使用

    第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...

  9. 爬虫scrapy模块

    首先下载scrapy模块 这里有惊喜 https://www.cnblogs.com/bobo-zhang/p/10068997.html 创建一个scrapy文件 首先在终端找到一个文件夹 输入 s ...

随机推荐

  1. 面向对象(OPP)的三大特征之 继承

    OPP(面向对象编程的缩写)的三大优势: 1.继承 2.封装 3.多态 一.继承 1.继承:是两个对象之间的一种关系 a继承b 例如王思聪继承王健林 在OPP中继承是描述类与类之间的一种关系 2.继承 ...

  2. Linux--磁盘检查简单介绍

    系统莫名其妙的掉电或磁盘发生问题非常可能导致文件系统的错乱,文件系统若发生错乱,可以使用fsck(file system check)命令进行检查. 使用权限:root用户 选项和参数: -a:检测到 ...

  3. 13_Hive优化

    Hive优化 要点:优化时,把hive sql当做map reduce程序来读,会有意想不到的惊喜. 理解hadoop的核心能力,是hive优化的根本. 长期观察hadoop处理数据的过程,有几个显著 ...

  4. mysql 5.6.38 数据库编译安装

    一.系统环境: # cat /etc/redhat-release CentOS release 6.9 (Final) 二.mysql 编译安装: 1.安装依赖包: yum install -y n ...

  5. (6)python基础数据类型

    python的六大标准数据类型 (一)Number   数字类型(int float bool complex) (二)String 字符串类型 (三)List 列表类型 (四)Tuple 元组类型 ...

  6. 使用Tarjan进行缩点无向图

    int From[maxn],Laxt[maxn],To[maxn<<2],Next[maxn<<2],cnt; int low[maxn],dfn[maxn],times,q ...

  7. 【小知识】证明 $1$ 到 $n$ 的立方和公式

    scb 发明了小学奥数(确信) Formula \(\sum\limits_{i=1}^n i^3 = (\sum\limits_{i=1}^n i)^2\) Provement 构造一个矩阵 \(a ...

  8. vue.js中父组件触发子组件中的方法

    知识点:vue.js中,父组件的method中,触发子组件中的方法,获得子组件中的定义的属性 (1)子组件 : child_crud.js var html_child_crud= "< ...

  9. iView - DatePicker组件神坑,如何处理?

    最近使用iView - DatePicker组件时发现一些问题,明明设置是正常的日期时间格式,当需要使用这个时间的时候,页面却显示 Fri Jun 09 2017 12:00:10 GMT+0800 ...

  10. 部署ELK

    1.搭建客户机Nginx ls[root@nginx ~]# hostname nginx [root@nginx ~]# cat /etc/redhat-release CentOS release ...