您的位置:首页 > 数据库 > Mongodb

Mongodb 和python相连,从json文本中导入数据

2016-09-02 16:13 435 查看

Mongodb 和python相连,从json文本中导入数据

因为现在mogondb 升级了,pymongo的version 变成了3.3

现在是将用scrapy 爬到的zhihu用户数据存到mongodb中,所以代码如下:

#coding=utf-8

import pymongo
from bson.objectid import ObjectId
import logging
from datetime import datetime
import json
from time import mktime
import time
import sys

propertiesList =["follow_topics","followers","followees","logs","collections"\
,"name"
,"sign" \
,"location" \
,"business" \
,"employment" \
,"position" \
,"education" \
,"education_extra" \
,"description" \
,"agree" \
,"thanks" \
,"asks" \
,"answers" \
,"posts" ]
# print propertiesList

hostname = "localhost"
port_num = int("27017")
db_name = "scrapy"

try:
client = pymongo.MongoClient(hostname, port_num)
#选择数据库
db = client[db_name]
#选择数据的collection
collection = db.zhihu
except:
print "connection wrong"
count = 0
mongolist = []
try:
f = file("data_utf8.json")
for line in f:
# print line
s = json.loads(line)
#这里仅仅是为了改变一下文档的格式
for item in propertiesList:
if s.has_key(item):
if len(s[item])!=0:
s[item] = s[item][0]
else:
s[item] = None
# break

if s.has_key("asks"):
count +=1
mongolist.append(s)
except:
print "read to json worng",count
print mongolist
collection.insert_many(mongolist)
print count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python mongodb