Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Last active November 3, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manmohan24nov/3a585481a35a149260a454e23bf772ab to your computer and use it in GitHub Desktop.
Save manmohan24nov/3a585481a35a149260a454e23bf772ab to your computer and use it in GitHub Desktop.
In [1]: import tweepy
In [2]: # Twitter API credentials
...: consumer_key = "consumer key"
...: consumer_secret = "consumer secret"
...: access_key = "access key"
...: access_secret = "access secret"
In [3]: auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
...: auth.set_access_token(access_key, access_secret)
...: api = tweepy.API(auth,wait_on_rate_limit=True)
In [4]: # Define the search term and removes the retweets to avoid duplicate data
...: search_words = "#USElections2020"
...: search_words = search_words + " -filter:retweets"
In [5]: # Collect tweets
...: tweet_new =tweepy.Cursor(,
...: q=search_words,
...: lang="en",
...: result_type='popular').items(100)
In [6]: tweet_data=[]
...: # Iterate the tweets
...: for tweet in tweet_new:
...: tweet_data.append(tweet.text)
In [7]: tweet_data
['Nine Nigerian-Americans Contesting In United States Election On Tuesday | Sahara Reporters…',
'#SRPoll: Which Candidate Do You Support In The US Presidential Election?\n\n1. @JoeBiden - Democratic Candidate\n\n2.…',
'One Day To US Elections, @JoeBiden Leads As @realDonaldTrump Trails In Battleground States, Threatens To Head To Co…',
'Time Machine:\nAmazing that Martin Luther King Jr. was 31 years old when he gave this prescient and prophetic speech…',
'The best thing about #USElections2020 is how the NRIs are supporting the narratives for #JoeBiden of equality, free…',
'A reminder as you seek comfort food in the days ahead that calories don’t count if you don’t use a plate. #HandToMouth #USElections2020',
'Don’t forget the big #USElections2020 overnight show with me and a host of brilliant guests on both sides of the At…',
'When I tell you Trump is a 🇳🇬Politician you lot will say its nonsense.\nWọn n pin iresi in the abroad…',
'“In a choice between a clown and a gaffe-prone plagiarist tarred by his son’s alleged corruption, Trump deserves th…',
'OPINION: An eerily timely message to Americans, resonating across the decades, on the eve of the 2020 presidential…',
'In the coming days and weeks especially, it is critical that social media platforms apply their standards in a mann…',
'With just 2 days to go, what does my timeline think about the #USElections2020']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment