I am trying to save tweets directly from Twitter
into MongoDB
for retrieval later, but I keep getting error messages with regards to json
. Someone please help! Here is the sample codes with error messages.
searcher = keywordSearcher('','','') ## this is an object class
client = MongoClient('localhost', 27017)
db = client.test_dbase ## connect to database twitter
collection = db.rio_olympics ## create a collection object
result = searcher.getTwitterComment('Olympics 2016', '21 August 2016', 2) ## this is where I query Twitter Search API
py_dict = json.load(result)
post_id = collection.insert_many(py_dict)
error message:
Traceback (most recent call last):
File "/home/edidiong/myWorkSpace/keywordSearchTest.py", line 12, in <module>
class keywordSearchTest:
File "/home/edidiong/myWorkSpace/keywordSearchTest.py", line 45, in keywordSearchTest
py_dict = json.load(result)
File "/usr/lib/python2.7/json/__init__.py", line 287, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
2
Answers
Your
searcher.getTwitterComment
returns a dictionary and not json.That is why you get this error,
json.load()
is already called probably.The error message indicates you are trying to
load
a dictionary object. This means you already have a dictionary object. I’m not sure what the point of thejson.load
would be even if it did work.So you can probably just do: