Purpose: A program that utilizes changing twitter API’s to grab tweets(data) by using a hashtag or a set of phrases
HLL Used: Python
Information Needed: consumer key, consumer secret, access key, and access secret. You will need these when you connect to the Twitter API while writing code in python. Follow this link, https://developer.twitter.com/content/developer-twitter/en.html, to request the information needed to proceed.
The Script:
import tweepy
import json
twitter_cred = dict()
twitter_cred['CONSUMER_KEY'] = '***********************'
twitter_cred['CONSUMER_SECRET'] = '***********************'
twitter_cred['ACCESS_KEY'] = '***********************'
twitter_cred['ACCESS_SECRET'] = '***********************'
with open('twitter_credentials.json', 'w') as secret_info:
json.dump(twitter_cred, secret_info, indent=4, sort_keys=True)
with open('twitter_credentials.json') as cred_data:
info = json.load(cred_data)
consumer_key = info['CONSUMER_KEY']
consumer_secret = info['CONSUMER_SECRET']
access_key = info['ACCESS_KEY']
access_secret = info['ACCESS_SECRET']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)
maximum_number_of_tweets_to_be_extracted = \
int(input('Enter the number of tweets that you want to extract- '))
hashtag = input('Enter the hashtag you want to scrape- ')
for tweet in tweepy.Cursor(api.search, q='#' + hashtag,rpp=100).items(maximum_number_of_tweets_to_be_extracted):
with open('tweets_with_hashtag_' + hashtag + '.txt', 'a') as \
the_file:
the_file.write(str(tweet.text.encode('utf-8')) + '\n')
print ('Extracted ' + str(maximum_number_of_tweets_to_be_extracted) \n + ' tweets with hashtag #' + hashtag)
def get_favourite_count(handle):
user = api.get_user(handle)
return user.favourits_count