hey welcome back to Super data world
today we're going to show you how to
pull tweets from Twitter and it's a lot
easier than you might think so first to
get started we need to go to Twitter
Developers
and when you sign up at Twitter
developer so you'll get a screen to sign
up I've already signed up and you create
a project and for free you get the
different authentication keys that you
need for Twitter so for this project we
need an API key in secret and we need an
access token and secret so just go in
I'll put the link to the
developer.twitter.com in here just go in
and create a project you'll get these
keys and you're good to go so once we've
got those keys we can go into our code
and this code might look a bit
complicated but you can just copy and
paste this whole thing I have it on
GitHub it doesn't change so you just use
the same thing every time and I'll just
talk through what I'm doing and in the
code here and how you can customize it
so the first thing we want to do is we
want to install the Tweety API and this
allows us to get a tweet out and so
first of all I'm going to do is I'm just
going to run this um this Tweety API
this is going to install within my
collab so that's done foreign and then
the next thing I'm going to do is I'm
going to import Tweety which is what
I've just installed and import pandas
and then I'm going to put in my
authentication here so I've got it done
up the top I obviously don't want to
reveal my keys but when you put in your
and when you sign up for the Twitter
developers just put in those four Keys
here and then we need to pass those into
the Twitter API authentication key right
so this you just don't change the code
here just put in your keys here and then
just pass those in here we have to
instantiate the Tweety API so it's just
3p API send in the auth in here and then
we can build our search query so
essentially the search query is what you
want to search for tweets on I'm using a
Elon Musk and fire it in here and then
very important what we do is that we
filter retweets filter replace and
filter links because when you're doing a
Twitter project you want the tweets to
be original you don't want them to be
retweets of other people's tweets or
links or ads I'm going to set my number
of tweets equal 100 in here and then I
can get into the pulling the tweets
themselves so when doing a try accept
Loop in here and this is just because a
few things can go wrong so you can see
what's going wrong if something does go
wrong so essentially I'm going to get my
tweets by doing an API that search tweet
and then the query equals the search
query which I've instantiated up here so
we can change this to whatever you want
I'll change this to a few things later
on language equals English the count is
equal the number of tweets so I've got a
hundred here and the Tweet mode equals
extended this is important because if
you don't put the Tweet Motors extended
you can get a truncated tweets into your
data set so what I'm going to do now is
I'm going to set an attributes container
as a list comprehension and all this is
doing is really just pulling certain
things out of the Tweet I'll show you
how big the for each tweet the data
coming out is later and you'll see why
I'm doing this so I'm pulling out the
username the when the Tweet was created
the favorite counter the Tweet the
source of the tweet and the full text of
the tweet for tweet and tweets
essentially whatever tweets are coming
out of this this is just pulling what I
need out of it and you can you can
increase this if you want to get other
things out the tweets and I'll show you
what all is available now in a few
minutes then I'm creating columns for
each of these to put into my data frame
so I want this to be called user Day
created number of likes source of tweet
and the Tweet itself so I'm creating the
data frame here as doing the attributes
container which is the list
comprehension from this and then adding
The Columns in there so it's as easy as
just pressing play and this will pull my
tweets it'll happen in one second and
just to show you what this looks like
I've got my username My Day created
number of likes source of tweet and the
Tweet itself and just to show you about
all that comes out of this API this is
just one tweet data for one tweet and
these are all the variables that come
out of it so a lot of things you can get
out of a tweet and you can see that this
tweet was created at 739 which is the
same as this so this is all the data
that's coming out here
so to get these tweets out you can just
go from collab and Port files tweets DF
to CSV tweets.tweet and files.download
tweets.csv so that's if you want to get
them out into a CSV all you have to do
is that and then that CSV will run and
this is what my CSV looked like so I
have a
I have a row for each one of my tweets
in here so this is my 100 tweets all
containing
Elon Musk and fired so you get Elon Musk
and fired in there now if I want to
change the tweets all I have to do is
change the query so I've got Elon Musk
and fired in here I now want to look at
World Cup in Messi
so I'm going to put World Cup in here
and I can put Messi in here
I'm gonna run this
now my tweets DF
are all about World Cup and Messi so
really it's as simple as that and I
pulled 4 000 tweets for a project and I
found it quite easy so I thought I would
just share that with you hope you found
this video useful follow super data
World on YouTube and Tick Tock for more
data analytics tutorials