Hello everyone, welcome back to channel #ngodein in this video we will learn how to pull twitter data using python this is very useful for those who want to practice data analysis and machine learning so for those who haven't installed python yet please check this video, a tutorial about installing python using the easiest way okay, let's move on okay to get twitter API access you have to have a Twitter token you can get this Twitter token at apps.twitter.com you can just log-in using your twitter account here for those who don't have a Twitter account, you can sign up here first okay when you have successfully logged in there was an option to "create app", so choose it it will be directed to this page here you will first fill the name of your application it is up to you then the description, it is up to you then this Website URL is actually up to you but if you don't have a website just write placeholder.place or whatever you want, just try it okay, then skip this just go straight down write the description here about what you want to do with the Twitter API just say you're students, for study purposes then press the create button here Twitter will assess your application you should get the approval in less than a day you can check your email for this okay once it's approved by Twitter there will be your apps here all you have to do is click the details here you just click the key and tokens this is the tokens that you will use there is an API key and a secret key API so now you can connect directly to the Twitter API Now we move to Jupyter first okay let's start from the beginning just like usual here we will use the Tweepy library so type import tweepy then try to press shift-enter if an error that means there is no tweepy module not installed in your python yet now you can go to your command prompt if you use Winpython you can click on the winpython command prompt if you use normal python you can use your windows command prompt here type pip install tweepy so what is tweepy it is a library that can connect yout python to the Twitter API Okay, the download is complete well, if you guys come back here again and press shift-enter again no errors, because we already have the library okay, let's make a variable first api_key, api_secret_key, access_token, and access_token_secret So, what should we put here, you can check here here you click regenerate regenerate then below too then, just copy this access token move here, access_token continue to access secret token, copy move here then, just close it now all that's left is to copy the api key and copy the api secret then, press shift-enter done your api key has been stored in these variables I do not want to bother to censor this credential everytime, so i need to hide this now we use the tweepy library first let's store the api first we make a new variable tweepy.OAuthHandler then Here we enter the key api_key, api_secret_key then, press shift-enter we also enter the token auth.set_access_token open parenthesis, access ... wow ... is there a drag race outside access_token, access_token_secret then, we connect the API make variable api = tweepy.api enter the variable you create earlier press shift-enter So, we've set the api all we have to do is to call it we try to pull data based on the user first we make a variable userResults = api. this is the api that you made earlier api.user_timeline open parenthesis id = for example we want to find whose tweet For example, Pak Jokowi's tweet jokowi comma, how many tweets For example, just ten tweets, so that there won't be too much then, shift-enter done the results have been saved in this hasilUser variable i am serious it's really easy right? it's only one line if you don't believe it, let's try to print the variable userHasil shift-enter well, the results are already out This is Pak Jokowi's tweet this is Json's form, so maybe it's a bit difficult to read but we've succeeded in pulling the data so it's really easy, just this one line no crazy-long code now we clean-up the results first we use for loop for tweet in userHasil: 128 00:07:11,720 --> 00:07:17,180 print(tweet.text) shift-enter well as you can see the tweet has been cleaned, we don't need any other properties it was really crowded before it's simple right? just this one line what do you think? well, this was based on the user now we try based on search hasilSearch = api.search open parenthesis q = "q" is a query so what you want to query for example you want to find Jakarta q = "jakarta" so we are looking for keyword Jakarta comma, lang (language) it's language code, Indonesian code is "id" lang = "id", just 10 tweets for now, so we can pull it fast then, shift-enter success just replace this with hasilSearch shift-enter so now, the tweet that has the keyword Jakarta is printed but it's better if we show the user's name so we write here tweet.user.screen_name comma then we give it a space ups nevermind we put "Tweeted" colon, space then, let's try to shift-enter now the user's name has been shown Cocobase Tweeted about this imorjoy tweeted about this We have succeeded for pulling data based on user and based on search too later if you want to find other features besides these two things you can take a look at Twitter documentation okay that's how to pull Twitter data using python very simple right in the next video we will use this data to process more advanced output thank you for those who have watched those who want to ask, can write it in the comments below if this video is helpful please support me by pressing like and subscribe see you guys in the next videos