How to โ€œlog inโ€ to a website using the Python Requests module?

I am trying to send a request to enter a site using the Requests module in Python, but it does not work. I'm new to this ... so I can't figure out if I have to make my Username and Password cookies or some kind of HTTP authorization that I found (??).

from pyquery import PyQuery import requests url = 'http://www.locationary.com/home/index2.jsp' 

So, now I think I should use the "post" and cookies ...

 ck = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'} r = requests.post(url, cookies=ck) content = r.text q = PyQuery(content) title = q("title").text() print title 

I feel like I'm doing cookies wrong ... I don't know.

If it does not register correctly, the title of the homepage should appear on "Locationary.com", and if so, it should be "Homepage".

If you could explain to me a few things about requests and cookies and help me with this, I would really appreciate it .: D

Thank.

... It still doesn't work. Ok ... so this is what the HTML page of the homepage says before you log in:

 </td><td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_email.gif"> </td> <td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td> <td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_password.gif"> </td> <td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td> 

So, I think I'm doing it right, but the exit is still "Locationary.com"

2nd EDIT:

I want to be able to stay on the system for a long time, and whenever I request a page in this domain, I want the content to appear as if I were logged in.

+77
python python-requests pyquery
Aug 09 '12 at 22:12
source share
5 answers

If the necessary information is on the page to which you are redirected immediately after logging in ...

Instead, let's call your ck payload variable, as in the documentation for python requests ::

 payload = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'} url = 'http://www.locationary.com/home/index2.jsp' requests.post(url, data=payload) 

Otherwise ...

See https://stackoverflow.com>

+39
Aug 09 '12 at 22:21
source share

I know that you have found a different solution, but for those like me who find this question, looking for the same thing, it can be achieved using queries as follows:

First, as Marcus did, check the source of the login form to get three pieces of information - the address to which the form is sent, and the attributes of the username and password field names. In his example, they are inUserName and inUserPass.

Once you get this, you can use the requests.Session() instance to make a request to send the login URL in which your login details will be useful. Executing requests from a session instance is essentially the same as regular requests, it just adds persistence, allowing you to store and use cookies, etc.

Assuming that the login attempt was successful, you can simply use the session instance for further requests to the site. Cookies that identify you will be used to authorize requests.

Example

 import requests # Fill in your details here to be posted to the login form. payload = { 'inUserName': 'username', 'inUserPass': 'password' } # Use 'with' to ensure the session context is closed after use. with requests.Session() as s: p = s.post('LOGIN_URL', data=payload) # print the html returned or something more intelligent to see if it a successful login page. print p.text # An authorised request. r = s.get('A protected web page url') print r.text # etc... 
+199
Jul 13 '13 at 18:32
source share

Let me try to make it simple, suppose the URL of the site is http://example.com/ and let you need to register by filling in the username and password, so go to the login page, say http://example.com /login.php and look at the source code and look for the URL of the action that it will mark in the form something like

  <form name="loginform" method="post" action="userinfo.php"> 

now take userinfo.php to make an absolute url that will be http://example.com/userinfo.php ', now run a simple python script

 import requests url = 'http://example.com/userinfo.php' values = {'username': 'user', 'password': 'pass'} r = requests.post(url, data=values) print r.content 

Hope this someday helps someone.

+30
Feb 20 '15 at 12:07
source share

Find out the names of the inputs used in the form of websites for user names <...name=username.../> and passwords <...name=password../> , and replace them in the scenario below. Also replace the url so that it points to the right site to login.

login.py

 #!/usr/bin/env python import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) payload = { 'username': 'user@email.com', 'password': 'blahblahsecretpassw0rd' } url = 'https://website.com/login.html' requests.post(url, data=payload, verify=False) 

Using disable_warnings(InsecureRequestWarning) will disable any script output when trying to disable_warnings(InsecureRequestWarning) sites with unverified SSL certificates.

Additionally:

To run this script from the command line on a UNIX-based system, put it in a directory, i.e. home/scripts , and add this directory to your path in ~/.bash_profile or a similar file used by the terminal.

 # Custom scripts export CUSTOM_SCRIPTS=home/scripts export PATH=$CUSTOM_SCRIPTS:$PATH 

Then create a link to this Python script inside home/scripts/login.py

 ln -s ~/home/scripts/login.py ~/home/scripts/login 

Close your terminal, start a new one, run login

+2
Jan 27 '17 at 15:03
source share

The requests.Session() solution helped to enter the form with CSRF protection (as used in Flask-WTF forms). Check if csrf_token is csrf_token as a hidden field, and add it to the payload with username and password:

 import requests from bs4 import BeautifulSoup payload = { 'email': 'email@example.com', 'password': 'passw0rd' } with requests.Session() as sess: res = sess.get(server_name + '/signin') signin = BeautifulSoup(res._content, 'html.parser') payload['csrf_token'] = signin.find('input', id='csrf_token')['value'] res = sess.post(server_name + '/auth/login', data=payload) 
+1
Jul 27 '19 at 11:44
source share



All Articles