处理Redis里的数据
来源:互联网 发布:广汽丰田面试经验知乎 编辑:程序博客网 时间:2024/05/18 12:42
处理Redis里的数据
有缘网的数据爬回来了,但是放在Redis里没有处理。之前我们配置文件里面没有定制自己的ITEM_PIPELINES,而是使用了RedisPipeline,所以现在这些数据都被保存在redis的youyuan:items键中,所以我们需要另外做处理。
在scrapy-youyuan目录下可以看到一个process_items.py
文件,这个文件就是scrapy-redis的example提供的从redis读取item进行处理的模版。
假设我们要把youyuan:items中保存的数据读出来写进MongoDB或者MySQL,那么我们可以自己写一个process_youyuan_profile.py
文件,然后保持后台运行就可以不停地将爬回来的数据入库了。
存入MongoDB
启动MongoDB数据库:
sudo mongod
执行下面程序:
py2 process_youyuan_mongodb.py
# process_youyuan_mongodb.py# -*- coding: utf-8 -*-import jsonimport redisimport pymongodef main(): # 指定Redis数据库信息 rediscli = redis.StrictRedis(host='192.168.199.108', port=6379, db=0) # 指定MongoDB数据库信息 mongocli = pymongo.MongoClient(host='localhost', port=27017) # 创建数据库名 db = mongocli['youyuan'] # 创建表名 sheet = db['beijing_18_25'] while True: # FIFO模式为 blpop,LIFO模式为 brpop,获取键值 source, data = rediscli.blpop(["youyuan:items"]) item = json.loads(data) sheet.insert(item) try: print u"Processing: %(name)s <%(link)s>" % item except KeyError: print u"Error procesing: %r" % itemif __name__ == '__main__': main()
存入 MySQL
- 启动mysql:
mysql.server start
(更平台不一样) - 登录到root用户:
mysql -uroot -p
- 创建数据库
youyuan
:create database youyuan;
- 切换到指定数据库:
use youyuan
创建表
beijing_18_25
以及所有字段的列名和数据类型
- 执行下面程序:
py2 process_youyuan_mysql.py
#process_youyuan_mysql.py# -*- coding: utf-8 -*-import jsonimport redisimport MySQLdbdef main(): # 指定redis数据库信息 rediscli = redis.StrictRedis(host='192.168.199.108', port = 6379, db = 0) # 指定mysql数据库 mysqlcli = MySQLdb.connect(host='127.0.0.1', user='power', passwd='xxxxxxx', db = 'youyuan', port=3306, use_unicode=True) while True: # FIFO模式为 blpop,LIFO模式为 brpop,获取键值 source, data = rediscli.blpop(["youyuan:items"]) item = json.loads(data) try: # 使用cursor()方法获取操作游标 cur = mysqlcli.cursor() # 使用execute方法执行SQL INSERT语句 cur.execute("INSERT INTO beijing_18_25 (username, crawled, age, spider, header_url, source, pic_urls, monologue, source_url) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s )", [item['username'], item['crawled'], item['age'], item['spider'], item['header_url'], item['source'], item['pic_urls'], item['monologue'], item['source_url']]) # 提交sql事务 mysqlcli.commit() #关闭本次操作 cur.close() print "inserted %s" % item['source_url'] except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])if __name__ == '__main__': main()
阅读全文
0 0
- 处理Redis里的数据
- 善待Redis里的数据
- 善待Redis里的数据
- 如何处理集合里的数据 Python
- redis 大量数据的插入处理
- mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据
- 将dic里的数据写到model里,自动处理null数据
- 关于回调函数处理DLL里的数据问题
- fragment里网络请求,返回数据的处理!大家帮帮忙
- Redis 与 数据库处理数据的两种模式
- Redis 与 数据库处理数据的两种模式
- Redis 与 数据库处理数据的两种模式
- Redis 与 数据库处理数据的两种模式
- redis队列处理在PHP里的使用 star.Hou的红楼一梦
- 更新页面数据时,怎么处理库里的数据和调用方的数据
- Beego里Redis的连接
- nodejs里redis的用法
- 把数据库里的大表放到REDIS中(从Mysql将数据快速导入到redis中 )
- 有缘网分布式爬虫案例2
- 171110—题:【递归改写】
- 【云星数据---mesos实战系列003】:marathon实战007--marathon部署一个docker register
- 第14章 单元测试(控制器测试、路由测试、验证测试)
- 高精度的问题
- 处理Redis里的数据
- (转)keil+stm32+JTAG利用swd方式进行printf输出
- PHP 显示当前年月日的方法集合时间戳转化为时间格式
- HDU 2084 数塔
- ELK日志分析平台的搭建以及beats模式的搭建
- 爬虫入门-03-12306官网登录详解
- 多研究些架构,少谈些主义(4)-- 架构师是技术的使用者而不是信徒
- DrawerLayout侧滑菜单点击切换Xlistview
- the storage of information of web app