- [Instructor] Next up we're going to show you
how to search through recent tweets,
so again, Twitter makes available up to the last
seven days worth of tweets for a given account,
and you can use the API method search
to search for specific query strings
that you supply as an argument.
It's not guaranteed to give you back
all matching tweets, just so you know,
so you will get what is effectively a sampling of tweets
containing those query terms that you specify.
You're going to be using the search method,
which in turn is going to call
the Twitter Search API's tweets method,
and that is going to enable you
to get 15 tweets at a time by default,
but you can get up to 100 of those at a time.
Now, of course if you're going
to be requesting a lot of results,
then you're going to want to use a cursor object as well,
and in this case we won't be using a cursor object
because we're only going to request
a few results for demonstration purposes.
So, let me switch back over to my terminal window,
and you'll notice that I've already
pasted in a snippet of code here.
We have a file that we've provided for you
in this lessons examples folder
that is called tweetutilities.py,
and it has a method, or a function rather,
in it called print_tweets that we will be using
in this example to display the content of each tweet.
So, I'm going to import that function,
and I just want to show you that function quickly here.
So, this is the tweetutilities.py file,
which just has a number of functions that we predefined
for you that we use in subsequent examples,
and as I scroll down here this is
the definition of the print_tweets function.
It's going to receive a collection of status objects
representing the tweets that are returned in this example
that will be from the search API,
and for each of the tweets in that collection
we're going to get the user object
from the tweet and show the screen_name.
And then if the tweet is in the English language
we're going to display the tweet's text,
and if it's not in English and the language
is not UND, which means undefined,
then we're going to show you the original tweet's text
and we're going to translate that tweet
using TextBlob's translate capabilities,
so we did import the TextBlob class up above,
so we're going to create a TextBlob from the tweet's text,
then we're going to invoke the translate method
on that to get back the translated
string and display that string.
So, of course Twitter is used worldwide
and tweets come in in many different spoken languages.
If Twitter is able to recognize that language
it will encode the tweet accordingly,
and if it's not able to recognize it
it will list that at UND for undefined.
So, going back over to our session now,
the next thing we want to do is perform a search.
So, we're going to call the API method search,
and again, I'll keep stating this
throughout several of the examples,
we really should be using cursors if we're going
to invoke search and get a lot of results back.
In this case we're getting only three results,
so there really is no need to use a cursor for this example.
The important argument is the Q argument,
which represents the query string.
In this case we're looking for recent tweets
for the Mars Opportunity Rover.
So, we're going to execute that,
and now we have tweet objects back
and we can call out print_tweets method, or function rather,
and we can simply hand it the tweets object
that we just created in the previous snippet,
and it's going to then display those tweets to us.
And in this case we did happen
to get a tweet that was translated,
so it looks like this might be Spanish
or possibly Portuguese.
I don't speak either of those languages,
so I'm gonna guess Spanish based on what I'm seeing here.
But in any case, it was able to go ahead
and translate that into English
as well as part of the display here.
Now, separately we have the ability
to work with Twitter Search operators
to refine the searches that we're performing.
So, let me jump back over to the slides for a moment here,
and the search operators allow you
to create basically more complex queries.
I've actually already opened a browser window
for the Twitter Search homepage,
and the reason I did that is you'll notice right below
the search field here there's this operators link,
and if you click that it will take you to a page
that allows you to see all the different
search operators with examples.
I have some examples that I've pasted into my slides here,
so these are just some of the ways in which
you can use Twitter Search operators
as part of your query strings that you supply
to that API object's search method.
So, if you just specify words,
that's implicitly going to be a logical and operation.
So, if we search for "python twitter,"
it's going to look for tweets that
contain both "python" and "twitter."
If you include the word OR between words,
that's going to be your logical OR operator,
so it will find tweets that contain
"python" or "twitter" or both.
If you say "python" and you include
a question mark in the search string
it looks for tweets that are asking
questions about the specified topic.
If you specify a search term followed
by a minus sign and another term
it's going to find tweets containing the original term
that don't contain the other one,
so this particular one would look for tweets
that contain the word "planets"
but don't contain the word "mars."
Some other examples, if you include a smiley face emoji,
that's going to look for positive sentiment tweets,
so this is a little bit of sentiment analysis
in the context of Twitter Search.
If you put in a sad face you'll get negative
sentiment tweets on that specified topic.
You also can search based on timeframes,
so I can use the "since" keyword followed by
a colon and a date of the form shown here,
which is a four digit year, a month, and a day,
and it will find tweets on or after that timeframe,
and again, remember you can only do for the past seven days.
So, if today were, well, today is June 12th of 2019,
so I could go back as far as seven days ago for tweets,
so maybe I only want tweets as far back as two days ago
and I could specify a date if I would
like with the "since" keyword.
I can also look for tweets near a particular location,
so I can give it a string like a city,
in this case "New York City" and it will find tweets
that came from that general area or that vicinity,
and you can also use the "from" and "to" keywords
to specify the particular accounts
from which a tweet was sent
or to which a tweet was sent as well.
So, with that in mind we can go ahead
and do a demonstration of some Python,
or not Python, excuse me, Twitter Search operators.
So, let's say that we would like to search for tweets
from the NASA account since a particular date.
Now, I've gone ahead here
and changed this to June 6th of 2019.
Again, this is June 12th of 2019,
and I'm looking for a maximum of three tweets in this case,
so I'll go ahead and execute that.
And once again, I will call my print_tweets
function to display those tweets,
so although I'm not showing dates with each of these tweets,
all of these would've been within seven days
of the time that I'm doing this particular recording.
Now, you can also search for things like hashtags,
so of course if you're familiar with Twitter
hashtags are a way that people
point out important things in a tweet.
So, for example if I want to do a search
for the hashtag #collegefootball
I can use a pound sign as part of my query string,
and now it's going to look for tweets that include
#collegefootball as part of the tweet text,
and in this case we're just going to get two tweets,
and we'll call print_tweets once again.
So, in this case it looks like three,
but in reality one of these is a tweet
that has a line break in the middle of it,
so it's not really three tweets that came back.
It's two tweets in this particular case.