1
votes

Ive tried to look this up on google and here but I don't seem to find anything that helps. I would like to search for more than one keyword, such as "java" "python" and "ruby" but I'm not too sure how to go about this problem, it involves sentiment analysis. TIA

api = TwitterClient() 
# calling function to get tweets 
tweets = api.get_tweets(query = 'java' , count = 200) 

I expect to get output of all tweets containing the words java, python and ruby, right now I'm only getting tweets about java.

1

1 Answers

1
votes

So I'm not going to write out the entire code for you, but you absolutely can do what you're looking for using Twitter's standard operators

You can use these to build up a query string of your keywords to get what you want, so say you wanted tweets that contained java, ruby and python together you'd make your query

"java ruby python" 

Now say you wanted tweets containing any of those words, you could use logical OR, e.g:

"java OR ruby OR python"

Of course, now you've got to find a way to actually use these. The api.search() method should work for that. I believe you can still use this on its own, but that's generally discouraged now there's the cursor. This means you don't have to deal with the tweets being separated by pagination; it does it all for you!

So the bit of your code that does the search will look something like:

searchTerms = "java OR python OR ruby"
for tweet in tweepy.Cursor(api.search, q=searchTerms).items(10):
    #whatever you need to do here

So in the above tweepy.Cursor is essentially getting a list of status objects (each object is essentially all the information of a single tweet). They contain things like a tweet's text, the time it was posted, number of retweets etc. Therefore the tweet variable in the for loop is a single status object which you can extract the data you require from. The .items() at the end gets you individual status objects as opposed to a page of them. You can put a number in there to define how many tweets you want to return.

For more examples have a look here lots of different uses of the cursor there that should give you an idea of how it is used.

Some other useful links:

Tweepy Cursor Documentation - Short, but it'll give you the gist of the cursor.

Tweepy method docs this gives you info on all the tweepy methods, and lets you know the kind of searches you can perform.

Hope that helps. Best of luck with the sentiment analysis.