Search code examples
pythonjsontwitter

I'm unable to write tweets to a json file with _json due to AttributeError


This is my code:

 searched_tweets = []
 new_tweets = api.search(q=query, count=remaining_tweets, since_id=str(since_id), max_id=str(max_id))
 searched_tweets.append(new_tweets)

 def write_tweets(tweets, filename):

 with open(filename + ".json", "w") as f:
    for tweet in tweets:
        json.dump(tweet, f)
        f.write('\n')

 write_tweets(searched_tweets, "testfile")

And I get this error when I try to write tweets to a new json file.

AttributeError: 'SearchResults' object has no attribute '_json'

I'm using Python 3x.


Solution

  • You're appending the search result as a whole to the searched_tweets list, so that now consists of a single item.

    You should remove the searched_tweets stuff altogether and just pass new_tweets to your function.