国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Redis > 正文

分布式爬蟲處理Redis里的數(shù)據(jù)操作步驟

2020-10-28 21:34:18
字體:
供稿:網(wǎng)友

存入MongoDB

1.啟動(dòng)MongoDB數(shù)據(jù)庫:sudo mongod

2.執(zhí)行下面程序:py2 process_youyuan_mongodb.py

# process_youyuan_mongodb.py# -*- coding: utf-8 -*-import jsonimport redisimport pymongodef main(): # 指定Redis數(shù)據(jù)庫信息 rediscli = redis.StrictRedis(host='192.168.199.108', port=6379, db=0) # 指定MongoDB數(shù)據(jù)庫信息 mongocli = pymongo.MongoClient(host='localhost', port=27017) # 創(chuàng)建數(shù)據(jù)庫名 db = mongocli['youyuan'] # 創(chuàng)建表名 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

1.啟動(dòng)mysql:mysql.server start(更平臺(tái)不一樣)

2.登錄到root用戶:mysql -uroot -p

3.創(chuàng)建數(shù)據(jù)庫youyuan:create database youyuan;

4.切換到指定數(shù)據(jù)庫:use youyuan

5.創(chuàng)建表beijing_18_25以及所有字段的列名和數(shù)據(jù)類型。

6.執(zhí)行下面程序:py2 process_youyuan_mysql.py

#process_youyuan_mysql.py# -*- coding: utf-8 -*-import jsonimport redisimport MySQLdbdef main(): # 指定redis數(shù)據(jù)庫信息 rediscli = redis.StrictRedis(host='192.168.199.108', port = 6379, db = 0) # 指定mysql數(shù)據(jù)庫 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()方法獲取操作游標(biāo)  cur = mysqlcli.cursor()  # 使用execute方法執(zhí)行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事務(wù)  mysqlcli.commit()  #關(guān)閉本次操作  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()

總結(jié)

以上所述是小編給大家介紹的分布式爬蟲處理Redis里的數(shù)據(jù)操作步驟,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青岛市| 桃江县| 娱乐| 宜城市| 吴川市| 家居| 马鞍山市| 深泽县| 丰镇市| 城口县| 大邑县| 南皮县| 平果县| 茂名市| 江西省| 许昌市| 泸溪县| 弥勒县| 福安市| 吴桥县| 卫辉市| 峨山| 陆良县| 丘北县| 贵溪市| 延川县| 正定县| 满洲里市| 项城市| 柘城县| 姚安县| 峨眉山市| 岳普湖县| 曲麻莱县| 庄浪县| 龙州县| 扬州市| 赞皇县| 天气| 漳平市| 雷波县|