Study Python – Tweet scraping, writing and sentiment evaluation utilizing tweepy and textblob in python
Tweepy is open-sourced, hosted on GitHub and enables Python to communicate with Twitter platform and use its API. Tweepy supports OAuth authentication. Authentication is handled by the tweepy.AuthHandler class.
TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.
TextBlob’s polarity attribute tells the attitude of phrase on the scale [-1.0 to 1.0] where -1.0 stands for negative and 1.0 stands for positive.
TextBlob’s subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.
Here is simple way how to access twitter data and write it into CSV file and perform sentiment analysis using textblob library.
<br />import csv<br />import tweepy<br />from textblob import TextBlob<br /><br />#consumer key and consumer secret key you get from twitter when you create<br /># a new app at http://apps.twitter.com<br />consumer_key = 'xxxxxxxxxxxxxxxxx'<br />consumer_skey = 'xxxxxxxxxxxxxxxxxxxxxxx'<br /><br />#access token and access secret key you get from twitter when you create <br />#a new app at http://apps.twitter.com<br />access_token = '54xxxxx-KJxxxxxxxxxxxxxxxxxxxxxxxxxxxx'<br />access_token_sk = 'VNxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxo'<br />
To get your own consumer key, consumer secret key, access token and access secret key please goto:
- http://apps.twitter.com login using your own twitter credential
- Create a new app button there and enter app name, app description and check the I agree hit submit you’ll get the necessary details after successful submission
- Remeber when giving app name at twitter dec page never include tweet word and it also demand for app name to be unique just like twitter userid
<br />#creating OAuthHandler by passing consumer key and consumer secret key<br />auth = tweepy.OAuthHandler(consumer_key,consumer_skey)<br />auth.set_access_token(access_token,access_token_sk)<br /><br />#passing the auth to tweepy API which provide gateway to tweets<br />api = tweepy.API(auth)<br /><br />#opening a csv file<br />csvFile = open('result.csv', 'a')<br />csvWriter = csv.writer(csvFile)<br /><br />#receiving keyword you want to search for<br />public_tweets = api.search(input("Topic you want to analyse for: "))<br /><br />#running a for loop to iterate over tweets and printing one row at a time<br />for tweet in public_tweets:<br /> #write in a csv file<br /> csvWriter.writerow([tweet.created_at,tweet.text.encode('utf-8')])<br /> print(tweet.text)<br /> analysis = TextBlob(tweet.text)<br /> print(analysis.sentiment)<br /><br />
which will generate a fantastic csv file like this.