Reputation: 77
Im very new to python, I have been having trouble using Tweepy. I have been trying to create a web application that is able to data mine twitter based on what a user puts into the search field on the landing page. With all the data I collect I would like to carry out sentiment analysis and visualise the data on my web application. The following is my python script and html search form.
Outcome of the following code gives me 406 error when I run in terminal. I think this is because Im sending a variable to track, but Im not too sure. However when I had my own search term in the script worked and saved data to csv file.
I might be doing a few things wrong here if anyone has any advice please shoot.
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import twitter
import cgi
#
ckey =''
csecret = ''
atoken = ''
asecret = ''
class listener(StreamListener):
def on_data(self, data):
try:
print data
#tweet
#print tweet
#saveFile
saveFile = open('twitDB1full.csv', 'a')
saveFile.write(data)
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata', str(e)
time.sleep(5)
def on_error(self, status):
print status
form = cgi.FieldStorage()
searchterm = form.getvalue('search')
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=searchterm)
#Set tracker to equal an object which is filled by html search field
The following code is the html landing where the user enters there selected search word.
<div id="search">
<button type="button" class="close">×</button>
<form >
<input id="searchID" type="search" value="" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
Upvotes: 0
Views: 670
Reputation: 77505
By the Twitter documentation (which you should study carefully!), the response code 406 is:
406 Not Acceptable
Returned by the Search API when an invalid format is specified in the request.
Dump your request, and study if it adheres to the specifications.
For example, what if the search term is emtpy?
Upvotes: 1