Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Tweepy search results as JSON

I would like to have the search result from Twitter using Tweepy as JSON. I saw here that I should add a class to Tweepy code to make this feature works.

But when I look at Tweepy code, this is what I get :

class JSONParser(Parser):

    payload_format = 'json'

    def __init__(self):
        self.json_lib = import_simplejson()

    def parse(self, method, payload):
        try:
            json = self.json_lib.loads(payload)
        except Exception, e:
            raise TweepError('Failed to parse JSON payload: %s' % e)

        needsCursors = method.parameters.has_key('cursor')
        if needsCursors and isinstance(json, dict) and 'previous_cursor' in json and 'next_cursor' in json:
            cursors = json['previous_cursor'], json['next_cursor']
            return json, cursors
        else:
            return json

    def parse_error(self, payload):
        error = self.json_lib.loads(payload)
        if error.has_key('error'):
            return error['error']
        else:
            return error['errors']

So I am not obliged to hack its code since the fucntion already exists.

This is how my code looks:

from tweepy.parsers import JSONParser
for tweet in tweepy.Cursor(api.search,
                       q=hashtag,
                       include_entities=True,
                       rpp=100,
                       parser=tweepy.parsers.JSONParser()
                       ).items(limit):

This is the error I get :

   print (json.dumps(tweet))
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <tweepy.models.Status object at 0xb6df2fcc> is not JSON serializable

What should I undrestand from this error ? How can I fix it ?

like image 814
4m1nh4j1 Avatar asked Jun 02 '14 20:06

4m1nh4j1


1 Answers

If you use Cursor like this

import json
api = tweepy.API(auth)
max_tweets=100
query='Ipython'
searched_tweets = [status._json for status in tweepy.Cursor(api.search,  q=query).items(max_tweets)]
json_strings = [json.dumps(json_obj) for json_obj in searched_tweets]  

searched_tweets is list of JSON objects, while json_strings is a list of JSON strings

like image 185
Jameel Grand Avatar answered Oct 30 '22 06:10

Jameel Grand