How to “log in” to a website using Python's Requests module?

前端 未结 6 1494
别那么骄傲
别那么骄傲 2020-11-22 02:48

I am trying to post a request to log in to a website using the Requests module in Python but its not really working. I\'m new to this...so I can\'t figure out if I should ma

相关标签:
6条回答
  • 2020-11-22 03:06

    I know you've found another solution, but for those like me who find this question, looking for the same thing, it can be achieved with requests as follows:

    Firstly, as Marcus did, check the source of the login form to get three pieces of information - the url that the form posts to, and the name attributes of the username and password fields. In his example, they are inUserName and inUserPass.

    Once you've got that, you can use a requests.Session() instance to make a post request to the login url with your login details as a payload. Making requests from a session instance is essentially the same as using requests normally, it simply adds persistence, allowing you to store and use cookies etc.

    Assuming your login attempt was successful, you can simply use the session instance to make further requests to the site. The cookie that identifies you will be used to authorise the 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's a successful login page.
        print p.text
    
        # An authorised request.
        r = s.get('A protected web page url')
        print r.text
            # etc...
    
    0 讨论(0)
  • 2020-11-22 03:06

    Let me try to make it simple, suppose URL of the site is http://example.com/ and let's suppose you need to sign up by filling username and password, so we go to the login page say http://example.com/login.php now and view it's source code and search for the action URL it will be in form tag something like

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

    now take userinfo.php to make absolute URL which 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
    

    I Hope that this helps someone somewhere someday.

    0 讨论(0)
  • 2020-11-22 03:07

    If the information you want is on the page you are directed to immediately after login...

    Lets call your ck variable payload instead, like in the python-requests docs:

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

    Otherwise...

    See https://stackoverflow.com/a/17633072/111362 below.

    0 讨论(0)
  • 2020-11-22 03:14

    Find out the name of the inputs used on the websites form for usernames <...name=username.../> and passwords <...name=password../> and replace them in the script below. Also replace the URL to point at the desired site to log into.

    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)
    

    The use of disable_warnings(InsecureRequestWarning) will silence any output from the script when trying to log into sites with unverified SSL certificates.

    Extra:

    To run this script from the command line on a UNIX based system place 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

    0 讨论(0)
  • 2020-11-22 03:22

    The requests.Session() solution assisted with logging into a form with CSRF Protection (as used in Flask-WTF forms). Check if a csrf_token is required as a hidden field and add it to the payload with the 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)
    
    0 讨论(0)
  • 2020-11-22 03:24

    Some pages may require more than login/pass. There may even be hidden fields. The most reliable way is to use inspect tool and look at the network tab while logging in, to see what data is being passed on.

    0 讨论(0)
提交回复
热议问题