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
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
In [ ]:
In [4]:
#redirect_user = auth.get_authorization_url()
#redirect_user
In [3]:
#api.user_timeline(id="rohitsinghsking")
In [ ]:
Comments
Post a Comment