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,即请求阶段做] ...
随机推荐
- java——异常类、异常捕获、finally、异常抛出、自定义异常
编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...
- 转 oracle 默认自动统计信息 时间修改
############sql3: https://blog.csdn.net/dataminer_2007/article/details/41363417http://blog.51cto.com ...
- Django-1 简介
1.1 MVC与MTV模型 MVCWeb服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负 ...
- python set_index与reset_index的妙用
- inventor卸载不干净
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- dom4j使用
http://www.cnblogs.com/zfc2201/archive/2011/08/16/2141441.html http://www.blogjava.net/i369/articles ...
- NodeJS 开发应用
NodeJS 开发应用 使用的 Node 版本: V8.11.4 开发工具: VSCode 1.27.1 系统: Deepin 15.7 Desktop x64 项目结构 项目结构 Project i ...
- [转]Asp.net Core中使用Session
本文转自:http://www.cnblogs.com/sword-successful/p/6243841.html 前言 2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. ...
- jquery日期插件jquery.datePicker参数
1.效果图 2.引入JS.CSS文件 jquery-ui.min.css和jquery-ui.min.js文件 Includes: core.js, widget.js, mouse.js, posi ...
- Hibernate课程 初探多对多映射2-4 测试
package com.ddwei.test; import org.hibernate.Session; import org.hibernate.Transaction; import com.d ...