HowToDataScience : Scraping Twitter Data

ritvikmath · Beginner ·🌐 Frontend Engineering ·8y ago

Key Takeaways

This video demonstrates how to scrape Twitter data using Twitter API with Python, utilizing libraries such as Tweepy and Tweet PI, and tools like Anaconda and Excel.

Full Transcript

[Music] in this video we'll be learning and extract all the tweets that have a certain hashtag or set of hashtags on Twitter so for example the upcoming Aquaman movie has a lot of popularity on Twitter and now because of some recent trailer information that just dropped so let's say we want to find all the tweets that have hashtag Aquaman and have hashtag DC comics in them we'll go ahead and click enter here and we see that we get all the tweets that have hashtag Aquaman and have hashtag DC comics so scrolling through these tweets we see that we might want some other information on each tweet as well for example you might want the timestamp of tweet we might want information about the person that tweeted it for example their username but also such as how many followers they have we definitely want the text of the tweet and we also want information on all the other hashtags that are contained in this tweet you will need a valid Twitter account the Python programming language installed on your computer I'm using Python version 2.7 you can use Python version 3 with a few minor adjustments to your code and optional but highly highly recommended is the anaconda package installer this is something that's going to easily allow us to get a certain tool we need for this project so I'll include some information in the description below for how you can install this alright so the first step in our process is going to be visiting this create an application Twitter website in order to generate a series of access codes which basically tell Twitter hey I'm a valid Twitter user requesting this data please give it to me so it's pretty painless process to create this application let's just walk through it the first is give this application distinctive name so I'm going to call it my tweet scraping application go ahead and enter a description here so I'm going to say this is used to scrape tweets via hashtag okay this website you can just put a placeholder name so I'm going to put HTTP colon slash slash place holder comp this field here is optional go ahead and read this Twitter developer agreement if you would like to hit this check mark and create your Twitter application so give it a second here and your application has been created so that was pretty easy now you want to click on this keys and access tokens tab right here and you want to make note of four different coats so go ahead and copy them into a notepad file or put them somewhere safe the first is this consumer key the next is the consumer key secret right here the next you have to go down here and click on create my access token under token actions once you click on that give it a second scroll back down and you'll see two new codes appear when it's called access token which is right here and the last is called access token secret right here so copy all those down somewhere safe and try not to give them to anybody keep them as secret as possible because if someone has your access codes then they can start making requests to Twitter on your behalf and you don't necessarily want that all right so our final step before getting right into the code is going to be downloading a python library that we need that's gonna make our lives a lot lot easier when we start doing the code so that's why I said downloading this anaconda package installer is a really good idea because it's gonna make downloading this library a lot easier so go ahead and navigate into your anaconda terminal here go into your working directory for me that's just see users rittvik right here and all you have to type is pip install tweet hi tweet pi is a library I talked about so it's basically an interface between Python and Twitter that has a lot of pre built-in functions that will see that make our lives a lot easier click enter it's gonna go to the web and collect everything we read for a tweet pipe and that's it it says successfully installed tweet PI three point six point zero and that's literally all we have to do for this step alright without further ado let's jump right into the code and the code in this case could not be simpler thanks to the tweet pi library whereas in previous scraping videos we had to jump through the multiple functions and understand different kinds of components here there's only one function that we have to worry about and it's called search for hashtags now search for hashtag takes in five arguments the first four of which are very similar they're simply the consumer key the consumer secret access token and access token secret that we got from the Twitter application website the last one is simply the hashtag phrase which in our case is hashtag Aquaman and hashtag DC Comics now the first thing that this search for hashtags function does is creates an authentication object called auth which is created from are four different access codes and it's simply going to be used to tell Twitter that we are a valid user requesting this data now the next thing that this function does is creates an API object API stands for application programming interface but the best way to think about it is basically a language that we need to talk to Twitter in in order to request our data so that's why the tweet I library is so so useful and powerful because it has all this language built in we don't have to actually understand the language that Twitter requests data in because Tweep I will just convert our request into the language for us as long as we have passed in this authentication telling it that we are authorized to request this data the next thing we do is generate the name of the spreadsheet we'll be saving all our data too it's basically just created using the hash tags we want strung together using underscores and then the next thing we do is go ahead and open up that spreadsheet for writing alias it with W we can think of this as just the spreadsheet itself going forward the next thing we do is write the header row so each of the fields that we're going to be filling up the spreadsheet with us in our case that's going to be the timestamp of the tweet the actual text of the tweet the username of the person who posted this all the other hashtags in the tweet and how many followers this user has we get into this for loop here which says for each tweet that I gather and go ahead and write that tweet into my spreadsheet now the true meat of this function happens in getting this list of tweets that we actually care about and that happens right here the only two things that this tweet I doc cursor function which gets all those tweets takes in is first the API object that we created up there so remember this is a object which contains the language as well as the authentication that we need in order to talk to Twitter the next is of course the hashtag phrase that we passed in and here I add to filter out the retweets because I don't necessarily want every single retweet of a certain tweet but if you do you can just remove that I want only tweets that are in the English language you can change that or remove it if you want all tweets and I put here tweet mode equals extended because sometimes the text of the tweet gets cut off and I wanted to avoid that and I specified here I want only a hundred tweets right now you can always increase that if you want more and decrease it if you want less now for each tweet that I get back I go ahead and just extract the relevant piece of information from it so if I want the time it was created I want tweet created app if I want the whole text of the tweet I just do tweet da full text and a couple of cleaning up operations right here and similarly for every other field I want it's just some basic operation on the tweet I got back you can go ahead and read through that now let's go ahead and see this in action so I'm gonna go ahead and run this cell here and now it's just asking me for all the different codes that I extracted from the Twitter application website so I'll go ahead and speed that bit up for you guys now it asks me the hashtag phrase of course I want hash tag Aquaman and hash tag DC Comics know you can do a lot of things here you can change this and to an or if you want if you just want to match one of them you can also do a bunch of other things I'll include some documentation in the description below now go ahead and enter and it's done let's go ahead and see the results let's go ahead and open up our working directory we see there's a new file here called Aquaman underscore DC Comics the name of which remember was made using all the hash tags that we wanted strung together using underscores let's go ahead and open this up we can open it up in Excel now let's zoom in a little bit so you can see it better we see all the data that we want is right here so let me expand this so you can see it we see the time stamp for each tweet we see the tweet text we see the username of the person who tweeted this we see the list of all the hashtags that are in this tweet and we see the number of followers that this user has and as we want it we have exactly a hundred tweets in our results all right so all the code that was used in this video is freely available at my github so you can feel free to go through that at your own pace I'll go ahead and link that in the description for you guys now if you learn something in this video go ahead and leave a like and subscribe to my channel for more videos on how to data science

Original Description

Link To Code: https://github.com/ritvikmath/ScrapingData/blob/master/Scraping%20Twitter%20Data.ipynb Create New Twitter App: https://apps.twitter.com/app/new Download Anaconda: https://www.anaconda.com/download/ Twitter Operators: https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators.html Tweepy Documentation: http://docs.tweepy.org/en/v3.5.0/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ritvikmath · ritvikmath · 35 of 60

1 Math Team Update
Math Team Update
ritvikmath
2 Single Variable Calculus Volume of a Sphere - Proof 1
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
3 Single Variable Calculus Volume of a Sphere - Proof 2
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
4 Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
5 Multivariable Calculus Volume of a Sphere Proof - Double Integrals
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
6 The Euclidian Algorithm
The Euclidian Algorithm
ritvikmath
7 Proving the Chain Rule
Proving the Chain Rule
ritvikmath
8 Proving the Fundamental Theorem of Calculus Part 1
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
9 Proving the Fundamental Theorem of Calculus Part 2
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
10 Math Puzzle - Poison Perplexity
Math Puzzle - Poison Perplexity
ritvikmath
11 Math Puzzle - Poison Perplexity - Solution
Math Puzzle - Poison Perplexity - Solution
ritvikmath
12 Expected Value and Variance of Continuous Random Variables (Calculus)
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
13 Expected Value and Variance of Discrete Random Variables (No Calculus)
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
14 Array Method
Array Method
ritvikmath
15 Complex Power Series and their Derivatives
Complex Power Series and their Derivatives
ritvikmath
16 Distributions - Intro
Distributions - Intro
ritvikmath
17 The Poisson Distribution
The Poisson Distribution
ritvikmath
18 The Bernoulli Distribution
The Bernoulli Distribution
ritvikmath
19 The Binomial Distribution
The Binomial Distribution
ritvikmath
20 The Continuous Uniform Distribution
The Continuous Uniform Distribution
ritvikmath
21 The Geometric Distribution
The Geometric Distribution
ritvikmath
22 The Triangular Distribution
The Triangular Distribution
ritvikmath
23 The Exponential Distribution
The Exponential Distribution
ritvikmath
24 The Borel Distribution + Notes on Poisson Distribution
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
25 The Gamma Distribution
The Gamma Distribution
ritvikmath
26 The Normal Distribution
The Normal Distribution
ritvikmath
27 The Laplace Distribution
The Laplace Distribution
ritvikmath
28 The Chi - Squared Distribution
The Chi - Squared Distribution
ritvikmath
29 Overfitting
Overfitting
ritvikmath
30 Vector Norms
Vector Norms
ritvikmath
31 Truths Behind the Titanic : K-Nearest Neighbor
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
32 The Mathematics of Breakups
The Mathematics of Breakups
ritvikmath
33 Sillyfish
Sillyfish
ritvikmath
34 Finding Optimal Paths - Dynamic Programming
Finding Optimal Paths - Dynamic Programming
ritvikmath
HowToDataScience : Scraping Twitter Data
HowToDataScience : Scraping Twitter Data
ritvikmath
36 Decision Trees
Decision Trees
ritvikmath
37 Perceptron
Perceptron
ritvikmath
38 Naive Bayes
Naive Bayes
ritvikmath
39 K-Nearest Neighbor
K-Nearest Neighbor
ritvikmath
40 Evaluating Machine Learning Models
Evaluating Machine Learning Models
ritvikmath
41 Decision Tree Pruning
Decision Tree Pruning
ritvikmath
42 K-Means Clustering
K-Means Clustering
ritvikmath
43 Gaussian Mixture Model
Gaussian Mixture Model
ritvikmath
44 Data Science - Fuzzy Record Matching
Data Science - Fuzzy Record Matching
ritvikmath
45 Time Series Talk : Autocorrelation and Partial Autocorrelation
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
46 Time Series Talk : Autoregressive Model
Time Series Talk : Autoregressive Model
ritvikmath
47 Time Series Talk : Moving Average Model
Time Series Talk : Moving Average Model
ritvikmath
48 Time Series Talk : ARMA Model
Time Series Talk : ARMA Model
ritvikmath
49 Time Series Talk : ARCH Model
Time Series Talk : ARCH Model
ritvikmath
50 Time Series Talk : White Noise
Time Series Talk : White Noise
ritvikmath
51 Time Series Talk : Stationarity
Time Series Talk : Stationarity
ritvikmath
52 Time Series Talk : ARIMA Model
Time Series Talk : ARIMA Model
ritvikmath
53 Time Series Talk : Lag Operator
Time Series Talk : Lag Operator
ritvikmath
54 Time Series Talk : What is Seasonality ?
Time Series Talk : What is Seasonality ?
ritvikmath
55 Time Series Talk : Seasonal ARIMA Model
Time Series Talk : Seasonal ARIMA Model
ritvikmath
56 So ... What Actually is a Matrix ? : Data Science Basics
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
57 Derivative of a Matrix : Data Science Basics
Derivative of a Matrix : Data Science Basics
ritvikmath
58 Basics of PCA (Principal Component Analysis) : Data Science Concepts
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
59 Eigenvalues & Eigenvectors : Data Science Basics
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
60 The Covariance Matrix : Data Science Basics
The Covariance Matrix : Data Science Basics
ritvikmath

This video teaches how to scrape Twitter data using Twitter API with Python, and how to extract relevant information from tweets. It covers the process of setting up a Twitter developer account, installing necessary libraries, and using Tweepy to interact with the Twitter API.

Key Takeaways
  1. Visit Twitter developer website to generate access codes
  2. Create a Twitter application with a distinctive name and description
  3. Copy access codes and store them safely
  4. Install Tweepy library using pip
  5. Create authentication object
  6. Create API object
  7. Generate spreadsheet name
  8. Open spreadsheet for writing
  9. Write header row
  10. Run the cell to fetch tweets with specified hashtags
💡 Using Tweepy library simplifies Twitter API interaction by converting requests into the language Twitter understands, making it easier to scrape Twitter data.

Related Reads

📰
React Explained: JSX, Components, Virtual DOM & Diffing Algorithm
Learn the fundamentals of React, including JSX, components, virtual DOM, and diffing algorithm, to build fast and interactive user interfaces
Dev.to · Saravanan Lakshmanan
📰
The “Wiz” Merger: How Google’s Internal Framework is Rewriting Angular
Learn how Google's internal framework is improving Angular with server-side rendering and hydration
Medium · JavaScript
📰
Day-3 of Posting blog (Lists in html)
Learn to create ordered and unordered lists in HTML to structure content effectively
Dev.to · antony stark
📰
I Built a 100% Free, Frictionless Resume Builder with Direct PDF/Word Exports
Learn how to build a free and frictionless resume builder with direct PDF and Word exports, and discover the tools and techniques used to create it
Dev.to · Solangi Waqas
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →