skip to Main Content

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


  1. Your searcher.getTwitterComment returns a dictionary and not json.

    That is why you get this error, json.load() is already called probably.

    Login or Signup to reply.
  2. 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 the json.load would be even if it did work.

    So you can probably just do:

    py_dict = searcher.getTwitterComment("Olympics 2016", '21 August 2016', 2)
    post_id = collection.insert_many(py_dict)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search