Unable to login to Github or Facebook or any such site using Python requests?
Question: I am using the following code:

    
    
    import requests
    s = requests.session()
    
    login_data = dict(login='username', password='my_password')
    s.post('https://github.com/login/', data=login_data)
    
    r = s.get('https://github.com/')
    print(r.text)
    

The content printed is from the login page and not the page after that. I have
also tried:

    
    
    import requests
    
    url = 'http://facebook.com'
    values = {'email': 'username', 'pass': 'my_password'}
    
    r = requests.post(url, data=values)
    print(r.text)
    

Both times, the data printed is from the login page and not the p[age after
that what am I doing wrong?


Answer: The `requests` module isn't a web browser. Although you _can_ do some pretty
cool stuff with it there are situations where it isn't an appropriate tool.

Many websites use security on their forms to reduce abuse. A popular strategy
is to use a [cross-site request forgery
token](https://en.wikipedia.org/wiki/Cross-site_request_forgery#Prevention).
This is a hidden field whose value is generated server-side and then included
in the form. When the form gets submitted the server can make sure that the
hidden input is present and valid.

This is one strategy for preventing submission of the form from somewhere
else. If you look at the source for GitHub's login page you'll see a hidden
input inside the form named `authenticity_token` which is probably used for
exactly that.

If you want to play with `requests`' POST feature you can use something like
<https://httpbin.org/>. Here, you should be able to POST a message and get an
appropriate response.

For interacting with GitHub or Facebook you should use a
[library](https://developer.github.com/libraries/) or a [published
API](https://developer.github.com/v3/).

