#100DaysOfCode in Python Transcripts
Chapter: Days 58-60: Twitter data analysis with Python
Lecture: Get all tweets with tweepy.Cursor
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Alright, let's do some coding. Finally. First, let's import the modules we're going to use and do some set up.
0:09
Then, I'm going to define a namedtuple called Tweet. And next, I'm going to set some global variables, which, in Python, are uppercase,
0:24
and words divided by underscore. So, we're going to look at Twitter data from our account and here are the environment variables explained
0:32
in last video, loaded into the notebook. And you can use os environment or os.environ and you can make a script in your favorite editor.
0:43
Or follow along in the notebook but notice to load a virtual environment in iPython, there is some set up you might need to do.
0:53
So, here's a link and a command you can run to get the virtual environment loaded into your notebook. That's all set. Lets dive straight into getting
1:03
PyBite's Twitter history which is over two thousand tweets. And we're going to look at the most popular tweets by the number of likes and retweets
1:11
the most common hashtags and mentions and finally, create a nice Wordcloud and we will see that Tweepy is awesome in making this very easy.
1:21
Alright, let's make an API object first. Alright, and then let's define a function to get all our tweets.
1:48
Wow, that's a lot going on here. So, we use a Tweepy cursor, which is an efficient way to loop all over the tweets.
1:57
And, I'm going to access the user time line, which is basically all our tweets, screen name is PyBites and for now, I'm not going to show replies
2:07
because I need them later. I'm going to include the retweets, so, basically I get everything.
2:14
And that's good because we can always discard stuff later. But we can not put stuff back that was initially not there.
2:21
So, then to loop over it, I use the items on that cursor, we had a namedtuple at the beginning, with id text created likes and retweets fields,
2:30
and I'm just populating those and yielding each tweet one by one. So, this is basically a generator, which we covered in Day 16.
2:41
Then let's load it on to a list, which might take a while, but makes it easier to inspect. And let's see how big that is.
2:54
Great, so, we have 2400 tweets. Let's see what we can do with those tweets.