scrapy pipeline
pipeline的四个方法
@classmethod
def from_crawler(cls, crawler):
"""
初始化的时候,用以创建pipeline对象
:param crawler:
:return:
"""
pass def open_spider(self, spider):
"""
爬虫开始执行时,调用
:param spider:
:return:
"""
pass def process_item(self, item, spider):
"""
每当数据需要持久化时,就会被调用
:param item:
:param spider:
:return:
""" return item def close_spider(self, spider):
"""
爬虫结束执行时,调用
:param spider:
:return:
"""
pass
实例
import pymysql
from scrapy.exceptions import DropItem class ChoutiPipeline(object):
def __init__(self, db_conf):
self.db_conf = db_conf
self.conn = None
self.cursor = None @classmethod
def from_crawler(cls, crawler):
"""
初始化的时候,用以创建pipeline对象
:param crawler:
:return:
"""
db_conf = crawler.settings.get('DATABASE')
return cls(db_conf) def open_spider(self, spider):
"""
爬虫开始执行时,调用
:param spider:
:return:
"""
print('爬虫开始 ...')
self.conn = pymysql.connect(
host=self.db_conf['host'],
port=self.db_conf['port'],
user=self.db_conf['user'],
passwd=self.db_conf['password'],
db=self.db_conf['db'],
charset=self.db_conf['charset']
)
self.cursor = self.conn.cursor() def process_item(self, item, spider):
"""
每当数据需要持久化时,就会被调用
:param item:
:param spider:
:return:
""" sql = 'INSERT INTO articles(title, title_url, summary, create_time, url_md5)' \
' VALUES ("%s", "%s" ,"%s", "%s", "%s")' a = sql % (item['title'], item['title_url'], item['summary'], item['create_time'], item['url_md5']) try:
self.cursor.execute(a)
self.conn.commit()
except Exception as e:
print(e)
return DropItem() def close_spider(self, spider):
"""
爬虫结束执行时,调用
:param spider:
:return:
"""
self.cursor.close()
self.conn.close()
print('爬虫结束 ...')
注册配置文件
全局配置:
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'day1.pipelines.ChoutiPipeline': 300,
}
也可以控制某个爬虫执行那行那个pipeline
class ChoutiSpider(scrapy.Spider):
name = 'Chouti'
allowed_domains = ['dig.chouti.com']
start_urls = ['https://dig.chouti.com/'] custom_settings = {
'ITEM_PIPELINES': {'day1.pipelines.ChoutiPipeline': 1}
}
scrapy pipeline的更多相关文章
- scrapy Pipeline使用twisted异步实现mysql数据插入
from twisted.enterprise import adbapi class MySQLAsyncPipeline: def open_spider(self, spider): db = ...
- scrapy Pipeline 练习
class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...
- scrapy项目5:爬取ajax形式加载的数据,并用ImagePipeline保存图片
1.目标分析: 我们想要获取的数据为如下图: 1).每本书的名称 2).每本书的价格 3).每本书的简介 2.网页分析: 网站url:http://e.dangdang.com/list-WY1-dd ...
- Scrapy 下载文件和图片
我们学习了从网页中爬取信息的方法,这只是爬虫最典型的一种应用,除此之外,下载文件也是实际应用中很常见的一种需求,例如使用爬虫爬取网站中的图片.视频.WORD文档.PDF文件.压缩包等. 1.Files ...
- Python逆向爬虫之scrapy框架,非常详细
爬虫系列目录 目录 Python逆向爬虫之scrapy框架,非常详细 一.爬虫入门 1.1 定义需求 1.2 需求分析 1.2.1 下载某个页面上所有的图片 1.2.2 分页 1.2.3 进行下载图片 ...
- Scrapy:为spider指定pipeline
当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...
- Python爬虫从入门到放弃(十六)之 Scrapy框架中Item Pipeline用法
当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的python类,负责接收到item并通过它执行一些行为, ...
- 二、Item Pipeline和Spider-----基于scrapy取校花网的信息
Item Pipeline 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline ...
- Scrapy爬虫框架第七讲【ITEM PIPELINE用法】
ITEM PIPELINE用法详解: ITEM PIPELINE作用: 清理HTML数据 验证爬取的数据(检查item包含某些字段) 去重(并丢弃)[预防数据去重,真正去重是在url,即请求阶段做] ...
随机推荐
- 简单的todolist的demo
放上代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...
- 实现代码重启android app.
var Form1: TForm1; implementation uses System.DateUtils, Androidapi.JNI.GraphicsContentViewText, FMX ...
- centos 7 源代码 mysql-5.7.2 安装
CENTOS MYSQL 5.7 下载MySQL 5.7 https://dev.mysql.com/downloads/mysql/5.7.html#downloads cd /usr/local/ ...
- arm交叉编译sudo-1.8.6p7
1.交叉编译 # tar -xvf sudo-1.8.6p7.tar.gz # cd sudo-1.8.6p7/ # mkdir build # ./configure --prefix=/home/ ...
- IPC之msgutil.c源码解读
// SPDX-License-Identifier: GPL-2.0-or-later /* * linux/ipc/msgutil.c * Copyright (C) 1999, 2004 Man ...
- python面向对象、类、socket网络编程
类和对象 python3统一了类与类型的概念:类==类型:从一组对象中提取相似的部分就是类:特征与技能的结合体就叫做对象: 类的功能: 初始实例化: 属性引用: 1.数据属性: 2.函数属性: 对于一 ...
- 51Nod 1831 PN表
打出PN表来 发现合数除16,34,289都是赢 质数除2,17都是输 #include<bits/stdc++.h> using namespace std; bool prime(in ...
- P2634 树上路径长度为3的倍数的点对数 点分治
在计算答案的时候维护一个数组num num[i]为当前所有点距离根距离%3的数量 则当前块的答案为num[0]*num[0]+2*num[1]*num[2] #include<bits/stdc ...
- BZOJ 1005 prufer序列
给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? 第一行为N(0 < N < = 1000),接下来N行,第i+1行给出第i个节点的度数Di ...
- solr介绍
solr架构图: 以下是Apache Solr的主要构建块(组件) 请求处理程序 - 发送到Apache Solr的请求由这些请求处理程序处理.请求可以是查询请求或索引更新请求.根据这些请示的要求来选 ...