网站上的Python POST表单登录 [英] Python POST form login on website

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

问题描述

我正在尝试让这个登录表单在 Python 中与工作站点一起工作,因此我可以对站点数据进行一些抓取等操作.

现在,当我打印 r.text 的内容时,它与登录页面的 HTML 相同.网站没有任何进展.

还值得一提的是,我对在这里使用请求无动于衷.我只是发现 urllib 中的方法等很丑陋,而且我在此站点上找到的大多数信息都倾向于使用 Requests 来完成上述任务.

以下是我正在处理的信息.

<input id="tz_offset_5" name="tz_offset" type="隐藏"><table border="0" cellpadding="2" cellspacing="0" id="table_LoginPage_3"><tr><td valign="top"><input id="realm_16" name="realm" type="hidden" value="所有用户"><table border="0" cellpadding="2" cellspacing="0" id="table_LoginPage_6"><tr><td>电子邮件地址</td><td>&nbsp;</td><td><input id="username_5" name="username" size="20" type="text"></td></tr><tr><td>密码</td><td>&nbsp;</td><td><input id="password_5" name="password" size="20" type="password"></td></tr><tr><td></td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td><input id="btnSubmit_6" name="btnSubmit" type="submit" value="登录"><</td></tr></td></tr></表单>

下面是我目前使用的 Python.

导入请求url = 'https:///dana-na/auth/url_default/welcome.cgi'有效载荷 = {'用户名':'<scrubbed>','tz_offset':'-480','realm':'所有用户','密码':'<scrubbed>','btnSubmit':'登录'}session = requests.session()r = requests.post(网址,有效载荷)打印 r.text

解决方案

使用 data 关键字.来自文档:><块引用>

通常,您希望发送一些表单编码的数据——很像 HTML形式.为此,只需将字典传递给 data 参数.你的数据字典将在请求时自动进行表单编码制作:

payload = {'key1': 'value1', 'key2': 'value2'}r = requests.post("http://httpbin.org/post", data=payload)打印 r.text

此外,您发布的 HTML 表明该表单实际上将转到 login.cgi 而不是 welcome.cgi.您也可以在屏幕截图中看到这一点.

使用:

url = 'https:///dana-na/auth/url_default/login.cgi'

I'm trying to get this login form working in Python with a work site, so I can do some scraping and whatnot with the site data.

Now, when I print the contents of r.text it's the same HTML as the login page. No progress is made into the site.

It's also worth mentioning that I'm indifferent about using Requests here. I just find the methods and such from urllib to be ugly, and most info I've found on this site has leaned towards using Requests for the above task.

Below is the information I'm working with.

<form action="login.cgi" autocomplete="off" id="frmLogin_4" method="post" name="frmLogin" onsubmit="return Login(1)">
  <input id="tz_offset_5" name="tz_offset" type="hidden">
  <table border="0" cellpadding="2" cellspacing="0" id="table_LoginPage_3">
    <tr>
      <td valign="top">
        <input id="realm_16" name="realm" type="hidden" value="All Users">

        <table border="0" cellpadding="2" cellspacing="0" id="table_LoginPage_6">
          <tr>
            <td>E-Mail Address</td>

            <td>&nbsp;</td>

            <td><input id="username_5" name="username" size="20" type="text"></td>
          </tr>

          <tr>
            <td>Password</td>

            <td>&nbsp;</td>

            <td><input id="password_5" name="password" size="20" type="password"></td>
          </tr>

          <tr>
            <td></td>
          </tr>

          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>

            <td>&nbsp;</td>

            <td><input id="btnSubmit_6" name="btnSubmit" type="submit" value="Sign In">&nbsp;</td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</form>

And below is the Python I'm using at this point.

import requests

url = 'https://<scrubbed>/dana-na/auth/url_default/welcome.cgi'

payload = {
    'username':'<scrubbed>',
    'tz_offset':'-480',
    'realm':'All Users',
    'password':'<scrubbed>',
    'btnSubmit':'Sign In'
}    

session = requests.session()    
r = requests.post(url, payload)
print r.text

解决方案

Use the data keyword. From the documentation:

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print r.text 

Additionally, the HTML you posted suggests the form will actually go to login.cgi and not welcome.cgi. You can see this in the screenshot as well.

Use:

url = 'https://<scrubbed>/dana-na/auth/url_default/login.cgi'

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆