从外部URL检索json数据并在python中保存到mongoDB时出错

时间:2014-11-21 07:17:34

标签: python web-services mongodb

我需要帮助将从外部URL检索到的json数据保存到python中的MongoDB。    我写的代码片段是在下面粘贴的。任何帮助将不胜感激。我是这个网站的新人。

import urllib.request

import pymongo

client = pymongo.MongoClient("localhost",27017)

db = client.test_database #database where I intend to store data

Collection = db.samplecollection # documents collection
#read data from url
readData = urllib.request.urlopen('some url that returns json data')
#store the data read to a variable ( I don't know if a document in Mongo is equivalent to an object or a complex type)
test = readData.read() # I confirmed data is being read
#save data to MongoDB
db.Collection.save(test) # when I try to save data to mongoDB I get an error

'''
 Error message
 Traceback (most recent call last):
  File "C:\EzempilloPythonScripts\readFdaData.py", line 8, in <module>
    db.Collection.save(test)
  File "C:\Python34\lib\site-packages\pymongo\collection.py", line 282, in save
    raise TypeError("cannot save object of type %s" % type(to_save))
TypeError: cannot save object of type <class 'bytes'>
'''

print (test) # I can print the data

2 个答案:

答案 0 :(得分:1)

read()为您提供了一个字符串,而不是一个jsonobject。你必须自己解析它。

我认为请求lib对此更好:

import requests

jsonobject = requests.get('url').json()

以下是te docs:http://docs.python-requests.org/en/latest/

答案 1 :(得分:0)

我认为你的test变量是一个字符串,对吗?

您必须转换json格式。它应该是json格式。

import json
test = '[{"_id" : 1, "name" : "HELLO"}, {"_id" : 2, "name" : "Happy"}]'
db.Collection.save(json.loads(test))