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