Make your own scalper bot!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so i just wanted to do a quick recording of a small bot uh creation so this is about for purchasing in this case rtx 3080 from best buy so as you know rtx 3080s came out i guess a couple weeks ago maybe a week ago i forget now but uh it sold out within seconds so it turns out there were all these bots that were scalping them off you know various websites and so people like us who didn't have a bot uh were left stranded so i decided to make one so here it is so this is a very simple script in python so i'll walk through the line by line like what all this stuff does but basically it um opens uh a webpage to uh the particular product that you want to purchase and looks for a button uh add to cart button but if it's gray meaning it's disabled or it's sold out then it'll sleep some or it'll just wait a certain amount of time uh in this case three seconds and then do a refresh of the page and then look for the button again to see if it's um purchasable or back in stock if you want then it'll add it to the card once it's added to cart uh it's up to you to kind of jump on it and then do the checkout process so this is a very uh crude simple script so let's walk through it so let's start by showing you what the page looks like so here's the sample page so let me just get a new browser tab so i can go there so here's a reference rtx 3080 sold out and basically press f5 or refresh until this turns yellow and best cut best buys case and then if it turns yellow it'll click it and add it to your cart so that's pretty simple so let's go over uh from the top what does this script do this is just a time i need uh for sleeping in port of time for sleeping here three seconds selenium is what i'm using to automate the browser so whenever you automate a kind of commercial website um something like this where they're probably trying all kinds of stuff to prevent bots it's best to try and use something that's going to drive the kind of authentic browser main reason is uh they don't want uh scripters to automate low-level apis i mean that's the ideal way to kind of uh attack these things but generally they'll hide the apis or they'll obfuscate apis and make it hard for you to uh go at their apis directly so you're having to go through the ui so if you're going through the ui you might as well just go through something that automates a browser like this so selenium from my research looks like is the best candidate selenium is supported in various platforms including python and because it's such a popular and widely uh implemented solution they their architecture is kind of uh complicated shall we say so unlike some of the other web scraping tools or automation packages you need to have a external binary so in this case uh you can google all this stuff and you'll find it installation instructions for setting up selenium for various browsers but you can see this executable it actually needs an executable this is a windows box so it needs an executable for the script to be able to drive firefox effectively so i'm guessing it's kind of opening a socket in the local machine so that the python script or the python client selenium library can talk to the gecko driver exe but i mean i'm just guessing but that's so that executable can kind of do system calls to the firefox and and be able to kind of hook itself into the internals of firefox but anyway we don't care about that so all we need to do is make sure you follow some web guidance or tutorial on how to set it up i think let me see if i can find the one that i followed so just search for selenium um python installation for windows yeah so i think this is the one so you need to so pip install or pip3 3 install for if you have python 3 installs the selenium client package so this is the package then you need to install they call it drivers so driver is the kind of native system binary that you need so you can follow this and you're going to end up downloading this thing called geckodriver like i did there in windows case you just drop it into the same package same folder as your script so this is my script right here and it will get picked up and that's pretty much it actually so that's as far as you need for obviously you need firefox get the latest one i think i'm on pretty current okay so first thing you do is uh use the web driver and say firefox give me a new firefox object and at that point you just do get on the on the firefox driver and this is the url that i just uh opened up earlier and this whole business is just to do looping right so first i i want to have some variable that says like have i found the add to cart button yet no then i'm going to keep looking for it and the way i look for it is this so you say the same driver dot find element by class name and i'm looking for so where did i get this id or name so what you do is let's close this um so if you get into the browser debugger you can also i use the hotkey but you can use the menu so you go to web developer and inspector so that's another way to go there so once you're in the inspector you can kind of uh select the item of interest and this is the item of interest right here so you select that it goes to the html element of interest so in this case it's a button and this button doesn't have an id but it does have a class that kind of is unique enough so what i'm looking for is this button class id add or name add to cart button that's where i got it from right there so you could look for any of these other ones but this is the one that i sounded the most uniquely identifying excuse me all right so [Music] so selenium uh should have found the button if it doesn't find a button this thing will crash meaning it'll throw an exception and it'll just exit which is fine uh that means there's something wrong with the code or the page changed enough that you there's no longer a button element by with that class name um then you'll have to come in and do some more debugging and add some more code to handle that case but i haven't had that case so this is this is working great so the next thing i'm looking for is whether this is a button that's disabled or not so i did i did this earlier and so here's the difference between a good button and a bad button so notice the good the bad button which is what you're seeing right there has not only this class but it also says button disabled right and here's the here's the example of a good button and the way the way i got the good button is i just went to some other product so let's just go somewhere like this one i'm sure isn't so this button right here so if you go to that button notice it doesn't have a disabled but it does have just the add to cart button so there's no mention of a disable class name right so the way i detect whether the button uh is clickable meaning it's it's uh it's in stock is if it doesn't have this right so if that same element that i found the button that i found uh is in the list of the classes that it has for that button doesn't include this one then i know it must be healthy and ready to be clicked on which is like this case the yellow button alright so but if uh the button disabled is was found in the list of attributes of the class in this case i'm just doing a string search because this add to cart button get attributes returns the full string like this string so i'm just doing a string search is button disabled in that string if not or rather if it is then i need to wait right i need to uh retry so i'm gonna sleep for about three seconds and then refresh the page so that's pretty easy and then look for it again i guess i don't need to look for it again and go back to look for it again because look for it again happens up top okay um and that's it otherwise so if button disabled was not found then i just say button found that goes true at which point this while loop goes oh button found is true okay so then i need to exit once you exit all i have to do is click and you'll see what happens if i uh when i substitute a good one but let's just show you what this looks like when it's running so when selenium is automating a browser it adds this little cute robot thing up top so now it's going to wait three seconds and retry but to get there already there you go so we retried now another three seconds and i'll keep it try and this just goes on forever okay all right so let's stop it there so now let's show you a demonstration of what it looks like when it's when it found something so here's a link for a healthy uh product that's in stock so i'm just gonna flip this on and disable the other one so now let's try this one so this one is a demonstration of um looking at a product that's in stock and then and then show you that how it adds to cart so notice i clicked it it's adding it to cart and one showed up in the cart and that's it so at this point uh the script needs to send out a notification so that's the missing part because uh obviously you won't you're not going to be here like you know around the clock so after the click the script should send out some way to notify the user that you got the item in the in the card so you better come and check it out otherwise uh or check out rather because um it's probably going to get removed from your cart is there anything else to show no well the other thing that's missing here is this url should be sent in from the command line and ideally you would have like a script a parent script that would run this script uh based on like a list of pages you want to watch because you probably are not going to just watch this not this one but whatever the other one the rtx 3080 because there's probably about two dozen different types of cards that you want to watch so make a page or make a make a data file or text file of all the links and from that link or from the from that list of paid the links uh spin up um this script for each one of those links so it would have i don't know n number of browsers popping up and refreshing so those are the two missing things and uh i'll work on it next and then i'll upload more videos as well as i build up the script that's it all right thank you
Info
Channel: Jay Juch
Views: 33,023
Rating: 4.3126616 out of 5
Keywords: python programming, selenium, browser automation, webscraping, scalper bot
Id: ReTrkBEYEyE
Channel Id: undefined
Length: 16min 54sec (1014 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.