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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With