skip to Main Content

I am looking to extract tweets and write them to a CSV file, however, I cannot figure out how to get it to generate a file. I am using Tweepy to extract the tweets. I would like the CSV file to contain the following cells: User, date, tweet, likes, retweets, total, eng rate, rating, tweet id

import tweepy
import csv

auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")

api = tweepy.API(auth)

try:
    api.verify_credentials()
    print("Authentication OK")
except:
    print("Error during authentication")

def timeline(username):
    tweets = api.user_timeline(screen_name=username, count = '100', tweet_mode="extended")

    for status in (tweets):
        eng = round(((status.favorite_count + status.retweet_count)/status.user.followers_count)*100, 2)
        if (not status.retweeted) and ('RT @' not in status.full_text) and (eng <= 0.02):
            print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Low' + ',Tweet ID: ' + str(status.id))
        elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.02 < eng <= 0.09):
            print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Good' + ',Tweet ID: ' + str(status.id))
        elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.09 < eng <= 0.33):
            print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: High' + ',Tweet ID: ' + str(status.id))
        elif (not status.retweeted) and ('RT @' not in status.full_text) and (0.33 < eng):
            print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Very High' + ',Tweet ID: ' + str(status.id))
tweet = timeline("twitter")

with open('tweet.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow([tweet])

2

Answers


  1. You can look at https://docs.python.org/3/library/csv.html for the info on how to generate a csv file in Python. Quick exmaple:

    import csv
    
    with open('some_output.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["field1", "field2", "field3"])
    
    Login or Signup to reply.
  2. Your function get_tweets does not return a value but you are trying to retrieve a value from that function which would result in None. Also it looks like tweet value will be list of strings. writerow method from csv.writer should get list of items and not list of lists. I have modified your code to address those issues. Let me know if it works.

    def get_tweets(username):
        tweets = api.user_timeline(screen_name=username, count=100) 
        tweets_for_csv = [tweet.text for tweet in tweets]
        print(tweets_for_csv)
        return tweets_for_csv
    
    
    tweet = get_tweets("fazeclan")
    
    with open('tweet.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(tweet)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search