使用 Python 登录 https 网站 [英] Login to https website using Python

查看:30
本文介绍了使用 Python 登录 https 网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在 stackoverflow 上发帖的新手,所以请不要咬人!我不得不求助于创建一个帐户并寻求帮助以避免再把头撞在桌子上......

我正在尝试登录以下网站 https://account.socialbakers.com/login 在 python 中使用 requests 模块.似乎请求模块是要去的地方,但 session.post() 函数对我不起作用.我不知道这种类型的表格是否有什么独特之处,或者网站是 https://

登录表单如下:

<big class="error-message"><大><强></大></大><div class="item-full"><label for=""><span class="label-header"><跨度>您的电子邮件地址</span></span><input id="email" name="email" type="email"/>

<div class="item-list"><div class="item-big"><label for=""><span class="label-header"><跨度>密码</span></span><input id="password" name="password" type="password"/>

<div class="item-small"><button class="btn btn-green" type="submit">登录

<p><a href="/email/reset-password"><强>忘记密码?</a></p></表单>

基于以下帖子 如何登录"访问使用 Python 的请求模块的网站? 其中我尝试过以下代码:

url = 'https://account.socialbakers.com/login'payload = dict(email = 'Myemail', password = 'Mypass')使用 session() 作为 s:汤 = BeautifulSoup(s.get(url).content,'lxml')p = s.post(url, 数据 = 有效载荷, 验证 = True)打印(p.text)

然而,这只是再次给了我登录页面,似乎没有让我登录

我已在表单中检查我指的是输入电子邮件"和密码"的正确名称.我也尝试过明确地传递 cookie.建议使用 verify=True 参数来处理网站是 https 的事实.

我无法弄清楚此表单与链接帖子中的表单有何不同之处.

谢谢

将 p = s.get 更新为 p = s.post

解决方案

检查了网站.它发送密码的 SHA3 哈希而不是作为明文发送.您可以在 script.js 的第 111 行中看到这一点,该内容包含在主页中:

head 标签内.

因此您需要在发送 POST 请求时复制此行为.我发现 pysha3 库可以很好地完成这项工作.

所以首先通过运行 pip install pysha3 来安装 pysha3(给sudo 如有必要)然后运行下面的代码

导入sha3导入哈希库进口请求url = 'https://account.socialbakers.com/login'myemail = "abhigolu10@gmail.com"mypassword = hashlib.sha3_512(b"st@ck0verflow").hexdigest() #取密码的SHA3有效载荷 = {'email':myemail, 'password':mypassword}使用 session() 作为 s:汤 = BeautifulSoup(s.get(url).content,'lxml')p = s.post(url, 数据 = 有效载荷, 验证 = True)打印(p.text)

您将获得正确的登录页面!

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...

I'm trying to login to the following website https://account.socialbakers.com/login 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://

The login form is the following:

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

Based on the following post How to "log in" to a website using Python's Requests module? among others I have tried the following 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)

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

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.

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

Thanks

Edit: Updated p = s.get to p = s.post

解决方案

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 script.js which is included in the main page as :

<script src="/js/script.js"></script>

inside the head tag.

So you need to replicate this behaviour while sending POST requests. I found pysha3 library that does the job pretty well.

So first install pysha3 by running pip install pysha3 (give sudo if necessary) then run the code below

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)

and you will get the correct logged in page!

这篇关于使用 Python 登录 https 网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆