We’ll learn how to search tweets with the Twitter Search API, using Twit's GET method to get the search/tweets endpoint. We'll use the query and count parameters to get the search term(s) and number of tweets that we want. We'll learn how to get exact phrases, multiple words, one of several words, emoticons, hashtags, photos/videos, urls, and how to remove words from our results. We'll learn how to implement the safe filter and how to filter by media, website, or date. We'll learn how to get recent results, popular results, results by location, and results by language.
The search API returns for relevance, and not completeness. If you want all the tweets from a search term, you should use the stream API (which we'll go over in the next lesson).
[00:00] Let's take a look at the Twitter Search API. The basic syntax to use it is bot.getsearch/tweets. There are some parameters which we'll come back to and then your call back function.
[00:20] If there's an error, we'll print it out. Otherwise, we'll print our data. It's data.statuses. For parameters, we have queue, which is our query. For now, let's look for balloons. It's a good one. We can pass it account. For now, let's just get back five tweets. If we run this, that's a lot of data.
[00:52] Let's do data.statuses.foreachstatus, print out the text, print the username, the user screen name, and a new line. If we run this again, see what we get back. A bunch of weird tweets about balloons.
[01:27] The search API returns for relevance and not completeness. It's actually a sampling of tweets from the last seven days. If you want completeness, you should use the stream API, which we'll get into next lesion.
[01:41] What can you do with the search API? Like we saw, you can search for any word, you can search for two words. This will return tweets with both of those words in it. If you want tweets with this exact phrase, you just put it in quotes, and now we will return tweets with the exact expression.
[02:03] You can look for tweets containing one of two words. You can look for a word that appears in a tweet without another word. These are all joined together, so you can subtract multiple words.
[02:22] You can look for emoticons, look for some confusing tweets. You can look for hashtags. You can look for tweets to a user or from a user.
[02:52] You can start doing some interesting things with media and filters. If you want the word "dance" but you want to filter out any risky tweets you can say, "Filter safe." If you want to picture a video, you can say, "Filter media," and these tweets will include a picture or a video.
[03:21] If you want to Vine, you can say, "Filter:Vine." That will return vines. You can specifically ask for images and not video. If you want the tweets to have links, you can filter:links and these tweets will have a URL. If you want links from a specific website, you can say your query word URL:Amazon so that Amazon will be in the URL. You can search for questions. You can add a date.
[04:09] Lastly, there are a couple of additional parameters you can use. One is result_type, and you can pass it either recent or popular. There's also this cool geo code parameter, which takes the format latitude, longitude, and then a radius in miles, which will look for tweets originating from that area. There is also the lang parameter to look for tweets in a language other than English.