Login to https website using Python
<p>I'm new to posting on stackoverflow so please don't bite! I had to resort to making an account and asking for help to avoid banging my head on the table any longer...</p>

<p>I'm trying to login to the following website <a href="https://account.socialbakers.com/login" rel="nofollow">https://account.socialbakers.com/login</a> using the requests module in python. It seems as if the requests module is the place to go but the session.post() function isn't working for me. I can't tell if there is something unique about this type of form or the fact the website is https://</p>

<p>The login form is the following:</p>

<pre><code>&lt;form action="/login" id="login-form" method="post" novalidate=""&gt;
        &lt;big class="error-message"&gt;
         &lt;big&gt;
          &lt;strong&gt;
          &lt;/strong&gt;
         &lt;/big&gt;
        &lt;/big&gt;
        &lt;div class="item-full"&gt;
         &lt;label for=""&gt;
          &lt;span class="label-header"&gt;
           &lt;span&gt;
            Your e-mail address
           &lt;/span&gt;
          &lt;/span&gt;
          &lt;input id="email" name="email" type="email"/&gt;
         &lt;/label&gt;
        &lt;/div&gt;
        &lt;div class="item-list"&gt;
         &lt;div class="item-big"&gt;
          &lt;label for=""&gt;
           &lt;span class="label-header"&gt;
            &lt;span&gt;
             Password
            &lt;/span&gt;
           &lt;/span&gt;
           &lt;input id="password" name="password" type="password"/&gt;
          &lt;/label&gt;
         &lt;/div&gt;
         &lt;div class="item-small"&gt;
          &lt;button class="btn btn-green" type="submit"&gt;
           Login
          &lt;/button&gt;
         &lt;/div&gt;
        &lt;/div&gt;
        &lt;p&gt;
         &lt;a href="/email/reset-password"&gt;
          &lt;strong&gt;
           Lost password?
          &lt;/strong&gt;
         &lt;/a&gt;
        &lt;/p&gt;
       &lt;/form&gt;
</code></pre>

<p>Based on the following post <a href="http://stackoverflow.com/questions/11892729/how-to-log-in-to-a-website-using-pythons-requests-module">How to &quot;log in&quot; to a website using Python&#39;s Requests module?</a> among others I have tried the following code:</p>

<pre><code>url = 'https://account.socialbakers.com/login'
payload = dict(email = 'Myemail', password = 'Mypass')
with session() as s:
    soup = BeautifulSoup(s.get(url).content,'lxml')
    p = s.post(url, data = payload, verify=True)
    print(p.text)
</code></pre>

<p>This however just gives me the login page again and doesn't seem to log me in</p>

<p>I have checked in the form that I am referring to the correct names of the inputs 'email' and 'password'. I've tried explicitly passing through cookies as well. The verify=True parameter was suggested as a way to deal with the fact the website is https.</p>

<p>I can't work out what isn't working/what is different about this form to the one on the linked post.</p>

<p>Thanks</p>

<p>Edit: Updated p = s.get to p = s.post</p>

<p>Checked the website. It is sending the SHA3 hash of the password instead of sending as plaintext. You can see this in line 111 of <a href="https://account.socialbakers.com/js/script.js" rel="nofollow">script.js</a> which is included in the main page as :</p>

<pre><code>&lt;script src="/js/script.js"&gt;&lt;/script&gt;
</code></pre>

<p>inside the <code>head</code> tag.</p>

<p>So you need to replicate this behaviour while sending <code>POST</code> requests. I found <a href="https://pypi.python.org/pypi/pysha3" rel="nofollow"><code>pysha3</code></a> library that does the job pretty well.</p>

<p>So first install <a href="https://pypi.python.org/pypi/pysha3" rel="nofollow">pysha3</a> by running <code>pip install pysha3</code> (give <code>sudo</code> if necessary) then run the code below</p>

<pre><code>import sha3
import hashlib
import request

url = 'https://account.socialbakers.com/login'
myemail = "abhigolu10@gmail.com"
mypassword = hashlib.sha3_512(b"st@ck0verflow").hexdigest() #take SHA3 of password
payload = {'email':myemail, 'password':mypassword}
with session() as s:
    soup = BeautifulSoup(s.get(url).content,'lxml')
    p = s.post(url, data = payload, verify=True)
    print(p.text)
</code></pre>

<p>and you will get the correct logged in page!</p>

python python-requests