scrapy Pipeline 练习
class WeatherPipeline(object):
def process_item(self, item, spider):
print(item)
return item
#插入到redis
import redis
import json
class RedisPipeline(object):
def __init__(self,host,port,password):
self.host=host
self.port=port
self.password=password
@classmethod
def from_crawler(cls, crawler):
return cls(
host=crawler.settings.get('RE_HOST'),
port=crawler.settings.get('RE_PORT', ''),
password=crawler.settings.get('RE_PASS', 'xxxxx')
)
def open_spider(self, spider):
pool = redis.ConnectionPool(host=self.host,password=self.password,port=self.port,db=3)
self.client=redis.Redis(connection_pool=pool)
# print(self.client)
def process_item(self, item, spider):
self.client.hmset(item['city'],dict(item))
# self.client.lpush('weather',json.dumps(dict(item)))
# self.client.sadd('weathers',json.dumps(dict(item)))
# return item
return item
#插入到mongoDB
import pymongo
class MongoPipeline(object):
collection_name = 'tianqi'
def __init__(self, mongo_host, mongo_db):
self.mongo_host = mongo_host
self.mongo_db = mongo_db
@classmethod
def from_crawler(cls, crawler):
return cls(
mongo_host=crawler.settings.get('MO_HOST'),
mongo_db=crawler.settings.get('MO_DB', 'weather')
)
def open_spider(self, spider):
self.client = pymongo.MongoClient(host=self.mongo_host)
self.db = self.client[self.mongo_db]
def close_spider(self, spider):
self.client.close()
def process_item(self, item, spider):
self.db[self.collection_name].insert_one(dict(item))
return item
#插入mysql 数据库
import pymysql
class MysqlPipeline(object):
def __init__(self,host,username,password,database,port,charset):
self.host=host
self.username=username
self.password=password
self.database=database
self.port=port
self.charset=charset
@classmethod
def from_crawler(cls, crawler):
return cls(
host=crawler.settings.get('MY_HOST'),
username=crawler.settings.get('MY_USER'),
password=crawler.settings.get('MY_PASS'),
database=crawler.settings.get('MY_DATA'),
port=crawler.settings.get('MY_PORT'),
charset=crawler.settings.get('MY_CHARSET'),
)
def open_spider(self,spider):
self.client=pymysql.connect(host=self.host,user=self.username,password=self.password,database=self.database,port=self.port,charset=self.charset)
self.cursor=self.client.cursor()
def close_spider(self, spider):
self.cursor.close()
self.client.close()
def process_item(self, item, spider):
self.cursor.execute("INSERT INTO weather (`sheng`,`city`,`hqiwen`,`lqiwen`) VALUES (%s,%s,%s,%s)",(item['sheng'],item['city'],item['hqiwen'],item['lqiwen']))
self.client.commit()
return item
scrapy Pipeline 练习的更多相关文章
- scrapy pipeline
pipeline的四个方法 @classmethod def from_crawler(cls, crawler): """ 初始化的时候,用以创建pipeline对象 ...
- scrapy Pipeline使用twisted异步实现mysql数据插入
from twisted.enterprise import adbapi class MySQLAsyncPipeline: def open_spider(self, spider): db = ...
- 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,即请求阶段做] ...
随机推荐
- 时间戳date 命令
时间戳date 命令 [chenxiaohao@iZbp1c0q444r0hp2gq8lhmZ server]$ echo $(date +%Y%m%d) ——————>今天 2018032 ...
- js写ajax并解析json
function down(t){ var req = createRequest(); //创建request req.open("GET","selectWord ...
- Murano Weekly Meeting 2016.05.17
Meeting time: 2016.May.17 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1 ...
- (转)Rsync命令详解
Rsync命令详解 原文:http://blog.51cto.com/irow10/1826249 说明: Rsync是linux/Unix文件同步和传送工具.用于替代rcp的一个工具,rsync可以 ...
- pat1002. A+B for Polynomials (25)
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- Tips In C
C语言中的使用操作 宏定义时使用do while防止语句的分离, 但是不使用与需要有返回值的语句, 这个时候可以参考第二条 宏定义时使用({}), ()加上{}的方式, 在代码中填写逻辑算法, 最后的 ...
- Qt 串口连接
Qt 串口连接 使用 Qt 开发上位机程序时,经常需要用到串口,在 Qt 中访问串口比较简单,因为 Qt 已经提供了 QSerialPort 和 QSerialPortInfo 这两个类用于访问串口. ...
- git分支合并冲突
合并冲突 如果你在两个不同的分支中,对同一个文件的同一个部分进行了不同的修改,Git 就没法干净的合并它们. 如果你对 #53 问题的修改和有关 hotfix 的修改都涉及到同一个文件的同一处,在合并 ...
- C#多线程进度条
public class ZyjProgressBar : System.Windows.Forms.ProgressBar { //用于跨线程访问控件的委托 private delegate voi ...
- knockout更新列表中的某条数据,knockout.js绑定数组时更新其中一条数据
knockout是一款前端实现MVVM的JS框架,仅knockout.js一个47kb的文件,相当实用,做前端无刷新页面,快速实现JS与HTML数据交互. knockout目前最新版:knockout ...