Twitter Sentiment Analysis

This is the code for twitter sentiment analysis using tweepy library and twitter API. tweepy
In [49]:
import quandl
import tweepy
import sklearn
import time
import matplotlib.pyplot as plt
from textblob import TextBlob
%matplotlib notebook
import pandas as pd
In [27]:
#w = TextBlob("i am feeling amazing")
#w.sentiment.polarity
In [2]:
cons_key='your key'
cons_sec = 'secret key'
acc_token='token key'
acc_sec = 'secret string'
auth = tweepy.OAuthHandler(cons_key,cons_sec)
auth.set_access_token(acc_token,acc_sec)
api = tweepy.API(auth)
In [73]:
#search for keyword and put in file
tweets = api.search(q="modi",count = 100,rpp = 1500)
l = []
for t in tweets:
    l.append(t.text)
df= pd.DataFrame(l,columns=['text'])
df.to_csv('D:/ai ml/data/modi.txt')
In [74]:
#sentiment analysis with graph
fig = plt.figure()
ax = fig.add_subplot(111)
fig.show()
i = 1
x,y = [],[]
for t in tweets:
    #print(t.text)
    a = TextBlob(t.text)
    #print(a.sentiment.polarity)
    #print(a.sentiment)
    pol = a.sentiment.polarity
    x.append(i)
    y.append(pol)
    ax.plot(x,y,color = 'g')
    fig.canvas.draw()
    time.sleep(1)
    i = i+1
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-74-e7d533bc999f> in <module>
     14     ax.plot(x,y,color = 'g')
     15     fig.canvas.draw()
---> 16     time.sleep(1)
     17     i = i+1

KeyboardInterrupt: 
In [ ]:
tweets = api.home_timeline()
for tweet in tweets:
    print (tweet.text)
In [88]:
#live follower
i = 1
fig = plt.figure()
ax = fig.add_subplot(111)
fig.show()
x,y = [],[]
while i<50:
    user = api.get_user('narendramodi')
    c = user.followers_count
    x.append(i)
    y.append(c)
    ax.plot(x,y,color = 'g')
    fig.canvas.draw()
    print(c)
    time.sleep(1)
    i = i+1
45294550
45294550
45294549
45294549
45294550
45294550
45294551
45294551
45294552
45294553
45294553
45294554
45294554
45294554
45294557
45294558
45294558
45294558
45294561
45294562
45294562
45294563
45294564
45294565
45294567
45294568
45294568
45294569
45294570
45294570
45294572
45294573
45294573
45294574
45294574
45294574
45294575
45294576
45294576
45294576
45294576
45294576
45294578
45294578
45294578
45294578
45294578
45294579
45294579
In [ ]:
 
In [4]:
#redirect_user = auth.get_authorization_url()
#redirect_user
In [3]:
#api.user_timeline(id="rohitsinghsking")
In [ ]:
 


Comments