How to create a Python Bot to auto-buy a GPU using Selenium Easily!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone today i'm going to show you how you can create easily a python bot that will help you to buy an item that is hard to buy like a gpu from a website like newegg.com i'm going to call the bot in python i'll use selenium webdriver and what is a python bot a python bot is a script that runs 24 7 or it can run 24 7 if you want and it can perform any kind of automated task that is doable via a web browser and with an internet connection bots have been around since the internet was invented the most famous bots you probably have heard are the google bots which find quality websites and quality videos like this one and you also have other types of bots like the stock market scalping boards automated shopping boards chat bots and the most infamous ones are the gpu scoping bots and that's probably why we are here anyway so without further ado let's get started so the first thing i'm going to do i'm going to assume you have no python installed in your machine and you don't have any development environment so i'm just going to show you how you can easily install anaconda currently i'm on a windows machine where you can do easily the same thing in a mac i'm going to show you how to install anaconda on a windows the first thing you do is to go to the anaconda website anaconda is actually very popular for anyone working with data science anyone who has done a python development will know about it the reason i'm going to use anaconda is because it's very convenient it has everything i need most of the things i need anyway so let's do that let's download the individual edition of anaconda so for windows 64 bits so we don't need to pay for it because we are just using this for individual use and just have to wait until it's done with it so it seems like he has finished downloading so i'm just going to run the install i have a previous version of anaconda installed but so it's going to ask me to replace it so let's have anaconda to the path environment variable better this way so that later on you don't have issues finding a conda in your environment so it has completed the installation i already have pycharm so i don't need it okay so now let's start anaconda so you can see here this is the anaconda navigator so before i i click on any of the applications here i want to create a new environment for whatever we are doing so i'm going to create an environment called gpu gpu by bot we're going to use python 3.8 so the advantage of using anaconda itself is first of all you can use any python version you want to have and you can have more than one version of python which you can pick depending on what you are doing another advantage is that you can create environments like this and you can use these environments to install the libraries that you require and you don't have to worry about incompatibilities with other libraries that might be installed in your system because this is an isolated environment so we're going to be installing some libraries that we might need so i'm going to open the command prompt and i'm going to do conda activate gpu by bot and you can see here on the left hand side there is a this is the name of the environment that is currently active so the first library that i'm going to install is selenium so i can do conda install selenium so next i'm going to install the selenium web basically we need a python chrome driver binary which will allow us to interact with chrome without having to do anything manually in the browser so conda install miner c forge so condo forge python chrome chrome driver binary okay now i'm going to install beautiful soup because we are likely to need it and panda just in case you need it with this we have everything we need to get started so i'm going to select this environment i already have it active i think so it's okay uh i'm going to install jupiter lab jupiter lab is an id that works on the browser and we can do some uh python development and see uh at the same time we can see all the the code running it makes it a lot easier to debug and uh yeah this is the best way to develop something yeah it's a it's a very useful id so gypto app is now installed and i'm going to start with the lab i'm going to create a new python notebook with python3 okay so now we are ready to get started so first thing we're going to do we're just going to make sure we are importing the selenium webdriver and the chrome driver binary then we just open the browser window so what i'm doing is opening a browser window and i'm setting a default here what does this mean implicitly which means when i'm trying to look for a link in a page i will wait up to 10 seconds until that link is available to click on because sometimes your script might be too quick and you might try to find a link while the page is loading and it fails because of that so implicit weight basically prevents that from happening i have to allow the access to the python script because a windows defender somehow didn't like it but i'll try again okay i was able to open the browser which is great so let's do one thing which is let's go to the new web website let's search for a gps let's say rtx 30 90. if there's anything in stock seems like it wow five thousand dollars that's a lot of money okay no wonder this one is in stock but i can use this as a test uh you can see there's other rtx 3090s here out of stock because they are a little bit cheaper but no it's too quiet expensive to be honest anyway let's use this as our our test subject so in this page you can see there's an auto card and i can click on it so what i'm going to do first of all i'm going to copy this url and i'm going to open the url here so so the url and then this is the method i'm using okay what i need to do first i need to open the browser again yeah so the browser again and now i can run that we get so you see i was able to open with selenium the product page okay so the first thing i want to do is actually i want to add this to cart okay so i'm going to inspect this so add to cart right so here you can see that there is actually this is not very straightforward the html element to get normally i will always try and get something like by id if you find an id in the html element that's unique but here they don't have any id and if you look at the classes the css classes they look very generic right so i don't necessarily want to use these css classes i can but i don't necessarily want to because it could be that there is another for example there's another button there could be a lot of buttons in this page so i only want to get this button here i'm in a hurry so there's a way to do this quite easily so i'm just going to right click now so i'm going to right click here copy and then there's some options here you can just copy the x path or the full x path the x path itself should be enough so i'm going to copy x path okay and now now i'm i'm back to gypto lab and i'm going to try and and click on an element in the page so find element by xpath and here be careful because there is this is a double quotes and this is also double code so i'm going to use single quotes outside so i don't have to escape anything here so let's see if i didn't get any of the syntax okay so i think it worked and now i can do add to cart dot click so this will actually click the button and it should add to cart let's see so he added to cart uh but of course there is there's further questions here so you can do no thanks or add to cart i don't i don't want to purchase protection so i'm just going to say no thanks so for that i also see that it's quite hard to find the right button here so i'm just going to use copy xpath now i'm going to go back here and this is put nofix button equal wd find element by xpath and you can see let's see if it finds it and then i just have to do quick all right so it has added item to cart now i want to go straight to the cart okay so i'm just going to click again here simple as that um make sure i get to the element and then i just do right click here copy xpath now i go back here it's the same so view card button go to wd find element by xbox okay and now to view card button dot quick okay so now we are in the checkout page you see another annoying question do you really want face masks almost 20 bucks i'm not interested so i'm just going to check this button here i could also say do not show it this again but in order for the script to work well uh ideally this should be exactly the same as it was so i'm not going to tick this box i'm just going to say i'm not interested i'll do the same copy xpath because it seems like it doesn't have any ids and it's a bit tricky and i'm just going to say not interested button equal to wd find element by xpath and now i just have to click shift enter is a shortcut so let's see now the pop-up is gone and i can start the secure checkout so same concept here if you want to do a secure checkout without paypal you can just inspect here so this is the button we want and copy xpath secure checkout button equal to wd find element by xpath okay and then secure but checkout button so click and now we have another screen which i would say that if you do have an account i would definitely use this account put your email address here and i would sign in and in this way you will not need to fill in all the address details that you have to do in the next bit so i want to go to the next step the same thing here let's check this button another button without an id it's possible to get there you probably can get there uh with some kind of uh x you can figure out the x path but i i don't really need to do that so i'm just going to copy the x path again so here guess checkout button dot equal to wd find element by xbath okay and then click okay so now we are in the next section so i need to click an address create an address again copy xpath anyway you would not have to do this if you already have an account so i'm just doing this just to show you how to to do it okay so see now i have an option to save my address what i need to do now is to use the send keys method to type my first name so let's just do that so first name dot send keys and i'll just call myself code okay so let's see if if it fills in okay so code then let's do the same for the last name so last name so i'm just going to copy next path again i know i'm i'm being a little bit lazy because normally you would try to in this case there's a name so i can search for the form and then get the right element uh you don't always have to use get element by xpath and in fact it might not be a good idea because if they if newegg does change their html slightly which might happen from time to time they might add a new element or whatever xpath is likely not to work but you know we're trying to do this quickly just to prove the concept um not really trying to to create something uh fully working so this is for the last name then we can just do last name ascend mental to address i'm going to have to make it up i do live in a united kingdom i don't live in the us but i have lived before in colorado of all places so let's try and do the address so copy xpath okay then address first line dot send keys so my house so i'm going to get the city so broomfield but this one is a bit more tricky because i need to pick something in the drop down so let's see how i'm going to do this so inspect so we have a select going to again copy xpath state code to okay and so for for a drop down uh you you need to use another method so this is a select element so we can select by index or we can select by value so i think value is probably the best option so you can see values so colorado uh in this case i'm going to use ceo so select by value by value colorado let's see what happens i need to tell selenium that this is a select so i'm just going to do select state i need to import so i can import here for this okay let's see if it's selected colorado yeah it's here and then i'm just going to you know if it cares about the zip code i'm just going to put the zip code as something in colorado so it doesn't okay copy xpath zip code and zip code dot clear i don't want to have any numbers there and then zip code dot send keys and i can put and i this is a zip code in corado and let's see then a phone number i'm just going to put a random phone number let's see if it has validation for phone numbers i don't know phone number equal to [Music] so i'm going to send put like 1 1 1 1 1 1 one one let's see if that works okay and i need the email address as well copy again okay and then email dot send keys codemento at example.com right so let's see if this works so it's the double quotes okay so let's see okay now everything's filled in and i'll just see if clicking save works i hope so click save to copy xpath again okay so wd dot find element by xbox so save button so save button dot click and it might give me an error it didn't like phone number so it wants a valid phone number so i think i need another digit see okay so it works then well the dress doesn't exist so this is just i'm going to say use it anyway because this is just for demo i'm not buying anything well copy xpath again let's do that and now i basically can go to the next step which is to the delivery copy html copy xpath again so wd find element by xpath okay so now it's very close so it's free shipping of course so i just want to go to payment and i'll stop at payment because i'm not buying it somewhere it's no point let's go to payment page okay okay so now go to payment page button and then double-click so all i'm doing here is assuming everything goes exactly as planned this is called the happy path where everything goes well and there's no problems there's no errors there's nothing going wrong and if that's the case this is the complete sequence from the start of the product page up to the final payment page in the checkout things can and do go wrong and that's where you might have to make this script a lot more complicated because you might have a captcha for example where they might want to find out if you're a bot or not in that case they will show you a capture and then you have there's ways to go around the capture but to be honest you really don't want to have that problem in the first place right so let's go to the payment page so let's click here so we are already there payment page and that's it and this is at the point where i'll stop because i don't want to use a credit card and i don't want to try and put a fake credit card but i think i've i've done enough to show you how you would create python bot to automatically buy something okay what i haven't really shown you so i've done step by step which is fine the thing is once you try and run this in one go you probably will have an issue so the problem with nowadays right so newegg for example they will be trying to prevent bots from buying stuff on their website right so if they see that you are executing all these steps very quickly right they will definitely block you from going ahead so what we'll have to do unfortunately we'll have to make things a lot slower so we i'm going to be adding between pages i'm going to add wait periods to ensure that we give enough time so that uh the new web website doesn't think we are bought okay but let me just put all this together now that we've done more or less what we wanted to do i'm just going to put make things a little bit more like uh put everything into one one box it's easier to visualize okay so now i have restructured my code a little bit so i've put things into blocks so the idea is i'm going to add clauses within the script for instance when i get to the product page i'm going to wait for 10 seconds right at the beginning you want to make sure you give enough pauses to make sure that you don't get blocked i mean at the end of the day like the quicker you try to go through through the checkout the more likely you are to be blocked and be seen as a bot so you don't want to do that you want to actually take your time like a normal person would someone who goes into the product page they will look at the page the product for a few seconds before they go to the next step and then we just start time between each between each step we basically either a timer we could even make this random by the way just to make it more fun you can actually create a randomizer that will randomize the amount of time you wait for something so you're less likely to be seen as a bot because bots will do things in a very synchronous fashion by default random will return a float between 0 and 1 and then we can specify a range i think so let's do that so the minimum is five seconds to eight and let's do like 15.00 okay so so you see that every time every time i execute this it will give me a random time okay so in this way i don't always wait for the same amount of time so we have now a random timer which i can use throughout so i'll just be adding this to every single page that whenever we move to another page uh or we click on another button we should always wait a bit of time okay so we probably should also do some delays within between uh fields whenever we fill in the address we want to make sure that we are we are waiting up to 10 seconds we are not seen as bots yeah it might be very slow but it's better to be so than being blacklisted or anything like that so last thing we should do another delay i should make this a function right um because we're using everywhere but for now this is just the the raw code you can tie the operators uh always so i'm going to restart the kernel so i i lose a session so let's run everything so i'm going to run other cells in below and see what happens fingers crossed i already failed i think it's already run so so run selected cells and below okay let's see what happens fingers crossed they will see it's taking its time okay so sometimes it happens that oh i made the mistake so i forgot to add some random sleep times here so i'm going to try again let's see if our script works from start to end using the randomized timers which will help new egg not banning us from from their site so let's see and this is where we finish basically i was able to create a selenium script which allows me to basically complete the checkout in the newegg website i use a selenium python x buff mainly that was it was really easy to do of course the key part of this is that i didn't just try to run the script as fast as i could because i know that i would have been blocked straight away so i added some randomized timers the reason i didn't just add constant timers is because there's some sophisticated software out there which some e-commerce companies will be using which will they will detect bots because they can actually and they can figure out that the pattern in which you access their website is not the same as the a real human would the way they would know that is because for example you would be requesting page after page within a second or not even that a normal human will open a page stay there for some time or type things slowly and it will be so it would be a lot slower than a normal bot right so i just with this code with the random timer it was really easy to just pretend that we are just human and okay so i i've shown you the code uh how you could actually buy a graphics card if it's available and you might be asking how do i know that the graphics card is available in the first place to buy so let's say i wanted to buy an rtx graphics card okay it's out of stock so anyway the way you know it's out of stock is because there is a css class here that says out of stock yeah and what you'll do you'll be monitoring your python script will be checking this page regularly and you will check for the css class and once you know that the item is in stock then the css class is gone and then you'll be able to buy this product using the code i've shown you if you um if you want to learn how to do that there's another video i created where i go in detail how you can actually check if an item is in stock or not in the new website as well so i'll put that link in the description so you can check it out and i hope you uh you found this tutorial useful i'll put this in github so you can access the code and try it for yourself and yeah let me know if you um if you found it helpful and i'll see you again soon happy coding
Info
Channel: CODE MENTAL
Views: 53,183
Rating: 4.8434381 out of 5
Keywords:
Id: 0jY4v4NDfcE
Channel Id: undefined
Length: 27min 23sec (1643 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.