存入MongoDB

1.启动MongoDB数据库:sudo mongod

2.执行下面程序:py2 process_youyuan_mongodb.py

# process_youyuan_mongodb.py
# -*- coding: utf-8 -*-
import json
import redis
import pymongo
def 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" % item
if __name__ == '__main__':
 main()

分布式爬虫处理Redis里的数据操作步骤

存入 MySQL

1.启动mysql:mysql.server start(更平台不一样)

2.登录到root用户:mysql -uroot -p

3.创建数据库youyuan:create database youyuan;

4.切换到指定数据库:use youyuan

5.创建表beijing_18_25以及所有字段的列名和数据类型。

分布式爬虫处理Redis里的数据操作步骤

6.执行下面程序:py2 process_youyuan_mysql.py

#process_youyuan_mysql.py
# -*- coding: utf-8 -*-
import json
import redis
import MySQLdb
def 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()

分布式爬虫处理Redis里的数据操作步骤

总结

以上所述是小编给大家介绍的分布式爬虫处理Redis里的数据操作步骤,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

标签:
分布式爬虫,redis,数据

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“分布式爬虫处理Redis里的数据操作步骤”

暂无“分布式爬虫处理Redis里的数据操作步骤”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。