skip to Main Content

I’m trying to get the below done, but keep getting an error. I know that I’m getting the information from Twitter because my monthly usage increasing. Any help would be appreciated.

Search Twitter for 5000 tweets mentioning the hashtag #covid and store them in a file titled tweets.txt. You should clean the tweets and make sure that one tweet is stored per line in the file, i.e. the tweets.txt file should exactly have 5000 lines (this can be done by replacing n in the tweet text with a “ ”).

This is what I have so far:

import tweepy

API_KEY = “My key will be here.”

client = tweepy.Client(API_KEY)

query = "covid"

response = client.search_recent_tweets(query)


for tweet in tweepy.Paginator(client.search_recent_tweets, query=query, max_results=10).flatten(limit=5000):
   
  save_file = open('tweets.txt', 'w')
  for tweet in tweepy:
    tweet = tweet.strip()
    save_file.write(tweet + ' ')
    
print(tweet)

save_file.close()

2

Answers


  1. You didn’t show error message so I can only guess what can make problem.

    You shouldn’t use for tweet in tweepy: but directy tweet.text

    # --- before loop ---
    
    save_file = open('tweets.txt', 'w')
    
    # --- loop ---
    
    for tweet in tweepy.Paginator(client.search_recent_tweets, query=query, max_results=10).flatten(limit=5000):
    
        item = tweet.text.strip().replace('n', ' ')
    
        save_file.write(item + 'n')
    
        print(item)
    
    # --- after loop ---
    
    save_file.close()
    
    Login or Signup to reply.
  2. I believe this to be an assignment question for a class you might be taking. This is because I have come across an assignment document which describes your question word for word. In this case, I would actually urge you to try to figure out the error on your own as this would help you hone your own skills, here are some useful POVs for the same.

    As for the answer, I agree with @furas and would provide you with some additional resources that may help in the future.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search