Build A Twitter Bot With Python That Gets You Followers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there my gorgeous friends let me ask you one simple question are you lonely where was I going with this okay if you're lonely then we're gonna build a Twitter bot with Python that's gonna automatically search for a keywords or a hashtag and it's gonna pull up all the tweets related to that and you're automatically gonna like them every few seconds it hopes that you're gonna get some followers back did that make any sense no well don't worry because it's gonna work and take a look let's see let's see how that looks so as you can see we can just add a hashtag here and we can run this and it's automatically gonna open up a browser for us it's gonna sign in its gonna log in I use my alias name again dimitri marco representing and as you can see it's gonna search for all the tweets it's gonna scroll down so it can get all the tweets and once that happens it's gonna open up each individual one it's gonna drop a like and it's gonna wait for a few seconds and then it's gonna keep going you can leave this up all night while you go to bed and sing ye boy so let's get going what what you want to see a magic trick don't you alright alright fine alright we have these two stupid rings and once you hit the ring on the other one it's gonna go through each other and Wow amazing Wow all right so let's shall we take a look let's take a look so we hit it and it's I swear to god it's worked the last time I'm not sure what's happening I okay before we get going you're actually gonna need a few things installed so let me quickly show you what you need all right so obviously we're gonna do this in Python so you're gonna have to download Python if you don't already have it python.org hit this one at 3.7 and you're good to go you're also gonna need Firefox if you don't have that downloaded install that and finally we're gonna need something called a gecko driver so go on gecko driver here alright and scroll Scroll scroll until you find these downloadable links here under assets okay and just get the one you're a computer user so Windows or Linux for Macs depending which operating system you are on I'm on Windows I'm gonna download this one okay let me kind of show you where you can put this so when whenever we run our Python app it's gonna open up a a Firefox browser alright so we need to put this into somewhere in Python so I have it here in downloads so this is the one we downloaded so the 64 make sure you unzip this one so unzip it and here is the unzipped one so I believe it's a an executable yeah it's just an executable so copy this and I'm gonna show you sure you were to bring it so we can go to see we can go to users I'm gonna go to dev ed and then add data and local and then programs and then python python 3.7 and finally here you can copy/paste this as you can see it's your get go driver alright so take a look up here again just to make sure that you don't get anything wrong alright and with this we can run a firefox browser up with python okay so that's the reason why we're doing all of these crazy things okay so let's install we're only gonna need one package here and just open up your turn terminal here's when you terminal and we're gonna say pip install see I already did it here already because I actually messed up my tutorial so I had to restart that's why you're seeing it up there but we're gonna pip install selenium all right hit enter I already have my install so nothing's gonna happen okay so let's import this package we're gonna say from selenium we're gonna import key webdriver all right this first one we're gonna see from selenium import this is gonna be a long one actually from selenium dot we're gonna see webdriver dot let me see what we need here common and then we need the keys all right and with this what we can do basically is we can add two inputs alright we can hit the enter key board or we can add an input when we log in and things of that sort so we're gonna say imports keys okay kind of long but hey what you gonna do alright enter enter and finally we're gonna import time and we're gonna use this multiple times across our application whoo okay so what do we need well we're gonna create a Twitter bot so I think there's a good idea is to create a class here that's gonna have different methods on top of it so we can call like login or search for the tweets and things of that sort so I'm gonna say class Twitter bot all right we're gonna create the class and we can initialize it so we can see the define here all right so function and we can call init like so with two underlines okay and if you're if you know JavaScript which you probably know if you've been following my channel this is kind of like a constructor function yeah that's pretty much it so here you would have this the best keyword and then whatever you want so like name age and things of that sort however in Python we use self so not this self okay and here I'm just gonna add username and passwords now I made a fake one I made the new Twitter so that's what I'm gonna use here but you can add your own just be careful not to push it up to github or anything because people are gonna see your password and username okay so that's the first thing we need so we defined this function here in it alright and here I'm just gonna say self dot username it's gonna be equal to a username okay self dot password it's gonna be equal to password and finally we can create an instance of that bought by saying self dot bought set that equal to webdriver dot Firefox like that with a big half and then we call this function okay and with this we can open up the page all right so we can open up Firefox we can visit a link and things of that sort so we would do something like bots don't get this URL here and thing okay so that's that's what this is okay do we need anything else on me see I think that's it perfect so that's one and then we can finally create one that's gonna login for us so a new method here on this class we can say login all right we can call self here all right so we're passing in this so again we can make reference to this or the soare this that's why you pass it in there alright and here we can see but equals to yourself the hot pot alright perfect and then we can see BOTS get and then we can pass nd Twitter URL so let's go on twitter.com copy this and paste it and here okay perfect and then what we can call is timed out sleep and what this is gonna do is just gonna pause your app for like two or three seconds for the web page to load up so nothing's gonna happen basically it's just gonna pause and we're gonna use this again multiple times just to wait the page to load up and now what we can actually do is we can test this out to see if it works so all we need to do is to create an instance of this class so I'm gonna say II D equals to Twitter bot alright and here we would pass in our username and password so our Twitter username and password I created a fake one here so I'm gonna say story of Ed yahoo.com yes Yahoo I know I'm old school and the the password is gonna be Dmitry Marco ninety-four okay and that's it now we're not using this right now we're gonna use it eventually here but I just want to see if this actually opens up the browser for us so finally what we can do is we can call this login function so we can say e d dot login okay and that's it and we can run this if we go up here and say Python app dot pi run alright we have an error thank god it's gonna say from Salina cannot import keys from selenium blah blah blah ok so what's happening right now is that we're waiting for three seconds just to make sure that the whole page loads up here you can kind of customize it if you have a slow internet you can add more to it just try that and see what works for you for me I have a pretty fast internet so this should be fine and the next thing we need to do is actually find these forms and fill them out so what we can do is press f12 and we can grab this little arrow garbage and we can hit here and that's gonna give us the input and it's gonna say class text input email input blah blah blah okay so we can actually use this class to select it so we can go here so after we wait for three seconds what we can do is we can create an email variable so I'm going to say email and we can set that equal to bot we're gonna use that bot again alright dot find underscore element underscore PI underscore class underscore name alright I don't know it is what it is alright everybody does it the way they want but they thought this was a good idea anyway so here you would just add the class and now this one has the class of email input all right it also has the text input but I believe this one also has text input so we're gonna just use email input here as we just say email input and that's it perfect let me actually show you another method because this one only has text input so we cannot really select it with the class because it's gonna select this as well so it's gonna select this as text templates and what we can do is we can also look for other keyboards we can look for ID which you can do by find element by ID we can also do let's take a look what we have here let me zoom in can you actually can you guys actually see this alright so here where is that thing click there we go we also have a class alright we can select it by name too so session password and that's pretty unique so that's what we're gonna do so I can create one with password too I'm gonna set this equal to I can say but not find element by name alright just by name and here I can pass in this session password so session password alright this one is in brackets so make sure you add that and what I actually want to do is before I submit this I want to make sure that these are empty before I actually add the values and submit it so this is just kind of a safe thing to do I'm gonna say email dot clear alright just in case there's something in it I'm gonna clear it and we're also gonna say passwords not clear and finally we're gonna add what we want in here with this keys so the way you can do that is we can say email dot send keys all right and rather than writing it manually here as you can see here we actually added it down here so we can just call self dot username and self dot password because it belongs to this entire class so we can just say self username all right well I'll probably email with a bit better here but all right we're gonna keep it like this now and we can say password dot sent keys and we can just say self let me make sure I add the dot here self dot password okay now before we submit it let's just give it a try so run this fight on add dot pi again let's see come on do the magic all right it's gonna wait three seconds and boom there we go it works so it fills everything out and the thing you can do is here is you actually don't need to look for this button you can actually just hit enter and the form is gonna submit so what we can do is down here we can just say password again dot send keys and what we can pass in here is keys all right so this thing that we imported from the webdriver keys dot and we can hit enter so as you can see we can hit arrow down we can do everything we want delete yeah you can pretty much do everything but what we need to do is say return all right so the enter keyword and that's gonna log us in and then what I'm gonna do is I'm gonna say timed out sleep all right I'm gonna give it three seconds to wait for everything to load up okay let's take a look if that works so run this again Python and PI and wish for the best all right come on there we go login and there we get we're logged in the mid-to Marco empty Twitter account feels bad man but it's okay okay so now what we want to do is a search for a hashtag and then pull up the tweets and do the rest hey so what we can do is we have this log in and now that we logged in we can just create another function here so let's go here and we can see Def and this is gonna be like tweets or something like that like tweet all right I'm gonna pass himself again because we're gonna use this we're gonna use the bots and I'm also gonna pass in hashtags so you guys can easily change the hashtag if you want at the end here you can just manually had it when we call this function okay so what we need to do again is we can say bot equals self-taught bot so we're getting that but again from up here and then what we need to do is get the link for hey let's see let's call this bad dog yet so we're gonna fetch another link here and it's gonna be let's just try one here all right I'm gonna log in I'm gonna log in with my account and I'm just gonna search for web development or something like that all right and it's gonna be this one search queue so that's the query web development blah blah blah all right so that's the one we need to replace so I'm gonna pass it in here I'm gonna delete everything I'm gonna close up the quotes here so I can add these pluses and I can just say hashtag okay perfect so we can just manually pass in here whatever we want all right there we go let me get rid of these plus all right so we're getting whatever we're passing in here and then we're gonna sleep again timed out sleep my favorite thing to do okay so let's call it down here so after we log in we're gonna say add dot like tweet and I'm gonna pass in the hashtag let's say web development okay let's take a look run this and hope it works - ii ii ii ii ii ii ii ii ii ii ii ii ii ii ii run it passin perfect and search there we go it works okay so now what I actually want to do is scroll down a bit because the way this loads is you have to scroll down and it loads more content so I want to load up quite a few pages here that I can like alright so that's the that's kind of what we want to do is to automatically make this scroll down for us a few times so the way you can do that is quite simple what you can we're gonna use JavaScript I bet you didn't expect this one so you can do is we can say bot dot execute underscore script so we can just run some JavaScript in here we can say window dot scroll to alright and we can pass in zero here like that and we can just say document dot body like that thought scroll height okay that's it that's gonna scroll down for us now this only Scrolls down once so that's we don't want to do that we want to scroll down multiple times so what we can do is we can create a small for loop here we can say for I in range one three all right we can you can pass ten or something more I'm just gonna do one tree here and then I'm gonna add these colons and then we can run this multiple time so I'm gonna add it in here so this is gonna scroll all the way down then I'm gonna make it sleep a bit so sleep like two seconds all right scroll down wait a bit for the content to load then scroll down again okay and after we do that what we can do is we can search for each tweet now if you take a look here if we select one of these it's just called tweet as you can see class name tweet so we can just get a tweet we can say tweets we can get all of them actually so let's say tweets equals bot dot find and we're gonna say find elements now not element elements by class name okay and we just we can just pass in tweet here alright the one from Twitter okay tweet okay now we have all of them very cool so now what we can do is we can get the link for each one so as you can see when we click on them you get redirected to whatever the guys name is status and the actual number here so generated I be there so to get that there's actually a thing here if we search it has data permalink path and that's the one we're looking for as you can see it says Chris data's blah blah blah okay but it doesn't have twitter calm before it so what we can do is we can make some magic here we can say links and we can set this equal to and we can just search for the elements so element I'm gonna say Elam get attribute and make sure I don't misspell this and we can just get the gathered by the attribute of data permalink path like so okay and we can make a for loop here so we can say for elements and tweets alright so what's happening here is we get these tweets and we search on each element and we get their data permalink path okay does it make sense hope it does we can actually console.log this out we can say print links now and let's see if this works run this let me scroll down a bit yeah this is gonna navigate signup it's gonna look for the hashtag and once it does it should throw us back all those permalinks with each individual tweets so let's see if it actually Scrolls down so it Scrolls down waits a bit Scrolls down and take a look brilliant perfect take a look we get back all those tweets so everything works just fine okay now what we can do is say for link and links so we can loop over all over all of these now the reason I'm doing this is so we can open it up again we can say bottle yet and I can open up each individual link however what I need to need to do before that is to actually get this Twitter calm and kind of interpolate it here with the string so plus link alright so this just takes towards calm and just adds one of these so it kind of loops over each one and what do we want to do we want to get each of these links so we get one now what we can do here is try to like the tweet and if it doesn't work then we can handle it with an exception so we can try all right and down here what what I want to do is actually get that heart button so here I want to get that heart button this one do like and this has a class name of heart animation so we're gonna get that and click on that so that's what we're gonna try to do so I'm going to say bot dot find element by class name that's fine and I'm gonna pass in the heart animation like that and what I want to do is say dot click alright simple as that and now I don't want this to go like every second so I'm gonna give it a timeout I'm not sure what Twitter uses how much you have to add here so it doesn't detect it as spam let's do ten seconds for now maybe do like a minute just to be safe I'm gonna add ten and because I'm a bit edgy okay and finally what I want to do is say except in case we have an error here I can say exception as X that's fine and I can just add a timed out sleep for like a minute or so so there we go and let's give this a shot so I'm gonna run this again let's go now here run Python Abdel pie and see if it goes it opens up wonderful add the email login perfect search okay now it should search for each individual one it's gonna scroll down a bit and boom opens up and look at that gave it a like perfect and now it should last like 10 seconds I believe that's the sleep we gave it and it's gonna go to the next one give it a like perfect there you go you can leave this running all the way you can leave it running till the morning so there you go thank you again very much for watching this episode and you know what you don't have to do this with Twitter you can do it with something else you can also do it with Instagram and wait you can actually sell this for like Instagram influencers that just want to get the ton of followers what you did I cut this video this is a brilliant idea don't watch it I'm gonna sell it myself all right everybody that's gonna be it until next time Dmitry Marco is out
Info
Channel: Dev Ed
Views: 213,816
Rating: 4.9415178 out of 5
Keywords: twitter bot, twitter bots, python project, python tutorial, python twitter, python selenium, how to python, python basics, create a twitter bot with python, twitter followers, get followers twitter, bot for followers
Id: 7ovFudqFB0Q
Channel Id: undefined
Length: 22min 33sec (1353 seconds)
Published: Sat Jul 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.