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,即请求阶段做] ...
随机推荐
- Android 中怎么重新启动APP或系统
重新启动应用程序,有两种方法,分别是: 1.通过ActivityManager来重新启动应用程序: ActivityManager manager = (ActivityManager)this.ge ...
- Spark Streaming简介
离线计算和实时计算对比 1)数据来源 离线:HDFS历史数据 数据量比较大 实时:消息队列(Kafka),实时新增/修改记录过来的某一笔数据 2)处理过程 离线:MapReduce: map+redu ...
- nexus 私服相关的配置
1 上传到nexus的配置 1 settings.xml <server> <id>releases</id> <username>admin</ ...
- C/C++中 static 的作用
在C中,有三个作用: 1.修饰全局变量: 作用是隐藏,也就是这个全局变量仅在本文件中可见. 2.修饰局部变量: 作用是扩展变量的生存期,令这个局部变量成为静态的. 3.修饰函数: 作用是隐藏,将此函数 ...
- Intellij IDEA 14 自动生成 serialVersionUID
1. Preferences > Editor > Inspections > Java > Serialization issues > Serializable c ...
- Spring Boot使用mongo的GridFS模块
1. GridFS简介 GridFS是Mongo的一个子模块,使用GridFS可以基于MongoDB来持久存储文件.并且支持分布式应用(文件分布存储和读取).作为MongoDB中二进制数据存储在数据库 ...
- JavaScript判断变量类型
使用JavaScript变量时是无法判断出一个变量是0 还是“”的 这时可用typeof()来判断变量是string 还是number来区分0和“”, typeof(undefined) == 'un ...
- js获取日期:前天、昨天、今天、明天、后天
前天,昨天,今天,明天,后天 <html> <head> <meta http-equiv="Content-Type" content=" ...
- uml的十三种图形
1.用例图:对系统的使用方式分类. 2.类图:显示类和它们的相互关系. 3.对象图:只显示对象及它们的相互关系. 4.活动图:显示人或对象的活动,其方式类似于流程图. 5.状态机图:显示生命周期比较有 ...
- (转)Entity Framework4.1实现动态多条件查询、分页和排序
原文:http://www.cnblogs.com/ahui/archive/2011/08/04/2127282.html EF通用的分页实现: 1 2 3 4 5 6 7 8 9 10 11 12 ...