Insert tweet search result in mongodb with python
Question: I'm trying to insert tweet search results into MongoDB using following code:

    
    
    import json
    import tweepy
    
    from pymongo import MongoClient
    
    ckey = ''
    consumer_secret = ''
    access_token_key = ''
    access_token_secret = ''
    
    auth = tweepy.OAuthHandler(ckey, consumer_secret)
    auth.set_access_token(access_token_key, access_token_secret)
    
    api = tweepy.API(auth)
    
    for data in tweepy.Cursor(api.search,q='test',since='2015-08-01',until='2015-08-10').items():
        client = MongoClient('localhost', 27017)
        db = client['twitter_db']
        collection = db['twitter_collection']
        tweet_json = json.loads(data)
        collection.insert(tweet_json)
    

But I got error message while parsing results as JSON:

    
    
    Traceback (most recent call last):
      File "twitter_past.py", line 28, in <module>
        tweet_json = json.loads(data)
      File "//anaconda/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "//anaconda/lib/python2.7/json/decoder.py", line 366, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    TypeError: expected string or buffer
    

Any idea for inserting the good stuff in my JSON format?


Answer: Because your cursor doesn't return you a JSON. It returns instances of
`tweepy.models.Status` model. And it obviously can't be parsed as JSON.

To get parsed JSON from the model you can use `data._json`.

