Login To Any Website using Python and Requests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys my name is exordium and in this video i will be showing you guys how you can log into any website and then after you're logged in do whatever you want on the website such as scraping or update a setting on your account or do whatever you're able to do when you're logged in normally on the website so i've already made a video on this but somehow it got taken down by youtube they didn't like that so i'm going to recreate the video i'm going to do it a little bit better so we're going to be using this website right here called giggle so it takes an email and a password to sign in and that's what we are going to replicate in our code so the first thing we need to do is to open up the the developer tools and we need to head over to the network tab and then also we need to click on this preserve log up here because if you don't click on this it's going to delete all of the logs once you get redirected which usually happens after you log in so make sure to click on this button and then we're going to try to log into this website so i've already got an account here so i don't need to sign up um i'm going to type in my password here which is query123 which is going to be changed after this video obviously so if i click on sign in it's going to send a lot of requests here but the first one is the off request which logs us into the website or actually the second one in this case because we are looking for the post request so this is the link or the api link that signs us into the website and if we scroll down we can see that it sends along our email and our password and then responds actually we can't see the response here but we can in decode later on which is the token because this website uses json web tokens and this video is going to show you both ways so some websites to use cookies so you don't really need to worry about web token and some websites uses the web token which is null here but it's actually populated once we're logged in so if you're using web tokens then you will see authorization down here and i'm going to show you guys how to persist a login using that as well so basically i'm showing you guys two ways but now once we are logged in we can grab this url and we can start to write some code in visual studio code so i'm going to create a new file i'm going to name it main.pi and the first thing we need to do is obviously import requests we're also going to be needing json to pick out the web token so that you're gonna need to import this as well if you're using that uh so the first thing we need to do is to define a function called login which is going to take in the mail and the password to log it into the website and we're going to be using request sessions in this video which basically allows us to persist cookies for this particular website it isn't really needed but it still doesn't hurt and i'm only using it so that it works on all websites so the next thing we need to do is to define the payload that we send to the server so basically this is the data that we see down here so i'm going to type email and then we're just going to pass in the mail and this has to be a string obviously and then we need to pass in the password as well now that's all for the payload now we can actually do the request right away so rest equals as dot post we're going to post a link that i copied and then we are going to post this as a json so why am i typing json on data well if you go back here and look at the request headers we can see that the content type is application application json and if it is application json that means that we have to send json to the server otherwise it won't accept it so if it says text plain or something else then you can probably use the data attribute but as i said it's using application json so we need to type json here instead and then we just need to pass in the payload we don't need any headers for this because we're only logging in and the authorization is going to be in the headers for future requests so that's why uh because of this website using the json web tokens i have to update the headers by typing s headers update and then this is going to be an object and we want to update the authorization which is going to come from the rest response so json loads and it takes in the rest.content and then we need to take out the token from this json object i'm going to show you guys exactly why i did this in a while i just need to get this working to show you guys some action so i'm just going to print out the rest.content which is going to say success if we're logged in and then i'm going to return this s which is the session object which will allow us to um i'm going to show you guys right here which will allow us to use this session object to make future requests on the website so here we take in the uh the mail so dan actually forgot what the mail was done that pop3 gmail.com and then the password which was qwerty123 so if i run this now it's going to print out this right here which is going to say success if the credentials are actually correct so let's try this out i'm going to click on the button up here and it says success true and now pay attention to the token entry down here um this is a token the json web token i was talking about which it returns to us because this website is using the json web tokens to make authorized authorized requests that means that i have to save this and put it inside the headers which i did up here so i'm just going to say that again if you're not using web tokens if you can't see anywhere where it says authorization on the website you don't need to do this at all i'm just showing you guys this so it applies to a lot more people now once we're logged in we can basically do anything we want so if i click on this button here and click on view profile i can edit my profile and change my location to anything so if i change my location to let's say usa i click on save then you see that it sends a me request and if we scroll down we will be able to see our authorization right here which as i said is what i put in the headers object but if you scroll down it sends location and then whatever location you set it to which i'm going to be doing as well so now we have the session object which is the logged in object actually i'm going to save this so r equals session dot patch we saw it was a patch request and then we want to take this url paste it in here and it's of course json as well so we want to pass in whatever we want to change so as we saw it was location and then the location we want so i'm just gonna type um let's say sweden which is the country on me right now um and that's basically it if i just print this to see that was successful are the content um we're going to be able to see if it worked and also i'm going to be logged out here because when i log in from another place it logs me out on the website so i'm just going to run this and it says success and it says success again which was this request so if i go back i have to log in to to actually be able to see the change if i go to my profile i'll be able to see that it says sweden so we just made an authorized request on the website after logging in using python and requests so that's basically all i want to show you guys in this video and if you found it helpful then please leave a like down below and subscribe and i'll see you guys in the next video you
Info
Channel: Exordium
Views: 3,557
Rating: 4.9615383 out of 5
Keywords:
Id: bM50i7sKwwM
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Wed Aug 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.