1
我需要幫助將從外部URL檢索到的json數據保存到python中的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