Search code examples
pythonjsonresttweepyinformation-retrieval

Saving tweets using tweepy in json format


How can we collect tweets using tweepy in json format and save to local disk. I need to input the json file in Solr for indexing and tokenization. here is the code i am using:

`

 import json
 import tweepy
 from tweepy import OAuthHandler

 ckey=""
 csecret=""
 atoken=""
 asecret=""

 auth = OAuthHandler(ckey, csecret)
 auth.set_access_token(atoken, asecret)

 api = tweepy.API(auth)

 data1 = api.search(q='politics', count = 1)`

Solution

  • You could try saving your data as a list and dump it into a json file, given that you already imported json.

    z = [x for x in data1]
    
    with open('your_data.json', 'w') as out:
        json.dump(z,out)
    

    alternatively, you could write z = [x for x in data1] as:

    z = list()
    for x in data1:
        z.append(x)
    

    /ogs