Python请求登录 - 登录页面没有错误返回 [英] Python requests login - the login page returned without an error

查看:23
本文介绍了Python请求登录 - 登录页面没有错误返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 请求登录网页,结果是相同的登录页面,没有任何登录错误.

这是登录表单:

<div class="登录"><div class="form"><div class="control-group"><label for="userInput" class="control-label required">Benutzer</label><div class="控件"><输入类型="文本"名称=登录名"最大长度=32"id="用户输入"><span style="display:none"类=帮助内联错误"id="VierCom_Base_Models_LoginForm_username_em_"></span>

<div class="control-group"><label for="VierCom_Base_Models_LoginForm_password" class="control-label required">Passwort</label><div class="控件"><输入类型=密码"名称=登录密码"最大长度=32"id="VierCom_Base_Models_LoginForm_password"><span style="display: none"类=帮助内联错误"id="VierCom_Base_Models_LoginForm_password_em_"></span>

<div class="control-group"><div class="控件"><按钮类型=提交"名称=btn登录"class="btn">登录</button>

</表单>

这是我的python代码:

导入请求进口重新导入 requests.utils、pickle导入参数解析导入时间从 bs4 导入 BeautifulSoup导入 csvuser_field = "登录名"pass_field = "登录密码"使用 requests.session() 作为 c:login_url = 'https://login.professional-cockpit.de/login.php'after_login_url = 'https://login.professional-cockpit.de/qlikView.php'用户名 = "*********"密码 = "*********"c.get(login_url)login_data = dict(user_field=USERNAME,pass_field=PASSWORD)###登录网站res = c.post(login_url, data=login_data)打印(res.text)

当我从浏览器登录时,这是请求标头:

POST/login.php HTTP/1.1主持人:login.professional-cockpit.de连接:保持连接内容长度:51缓存控制:max-age=0来源:https://login.professional-cockpit.de升级不安全请求:1用户代理:Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/57.0.2987.133 Safari/537.36内容类型:应用程序/x-www-form-urlencoded接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8推荐人:https://login.professional-cockpit.de/login.php接受编码:gzip、deflate、br接受语言:en-US,en;q=0.8,he;q=0.6,de;q=0.4Cookie:PHPSESSID=g28c793759o9nthgrhd0p99536

我尝试了所有方法并在谷歌上搜索了很多次.我将不胜感激.

解决方案

您的负载不是必需的.始终使用 chrome 的开发者选项卡(网络)将真正的发布/获取信息外包

使用此有效载荷

payload = {'loginName':user_field,'登录密码':密码字段,'btn登录':''}

你的字典看起来像这样:

payload = {'user_field':user_field,'pass_field':pass_field}

dict 的键与真实数据的名称不同,您错过了 btnLogin 字段.遗憾的是,对于任何虚假信息,网络服务器都会丢弃您的请求,因为它具有必须传递的特定硬编码"值.

I'm trying to login to a webpage using python requests and the result is the same login page, without any login error.

This is the login form:

<form method="post" action="login.php" id="yw0" class="well form-horizontal">
        <div class="login">
                        <div class="form">
                <div class="control-group">
                    <label for="userInput" class="control-label required">Benutzer</label>
                    <div class="controls">
                        <input type="text"
                               name="loginName"
                               maxlength="32"
                               id="userInput">
                        <span style="display:none" 
                              class="help-inline error"
                              id="VierCom_Base_Models_LoginForm_username_em_"></span>
                    </div>
                </div>
                <div class="control-group">
                    <label for="VierCom_Base_Models_LoginForm_password" class="control-label required">Passwort</label>
                    <div class="controls">
                        <input type="password"
                               name="loginPwd"
                               maxlength="32"
                               id="VierCom_Base_Models_LoginForm_password">
                            <span style="display: none"
                                  class="help-inline error"
                                  id="VierCom_Base_Models_LoginForm_password_em_"></span>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button type="submit"
                                name="btnLogin"
                                class="btn">Login</button>
                    </div>
                </div>
            </div>
        </div>
    </form>

This is my python code:

import requests
import re
import requests.utils, pickle
import argparse
import time
from bs4 import BeautifulSoup
import csv

user_field = "loginName"
pass_field = "loginPwd"

with requests.session() as c:
           login_url = 'https://login.professional-cockpit.de/login.php'
           after_login_url = 'https://login.professional-cockpit.de/qlikView.php'

           USERNAME = "*********"
           PASSWORD = "*********"
           c.get(login_url)

           login_data = dict(user_field=USERNAME,pass_field=PASSWORD)

           ### login to website
           res = c.post(login_url, data=login_data)

           print(res.text)

When i login from the browser, this is the Request headers:

POST /login.php HTTP/1.1
Host: login.professional-cockpit.de
Connection: keep-alive
Content-Length: 51
Cache-Control: max-age=0
Origin: https://login.professional-cockpit.de
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: https://login.professional-cockpit.de/login.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,he;q=0.6,de;q=0.4
Cookie: PHPSESSID=g28c793759o9nthgrhd0p99536

I tried everything and googled this many times. I would appreciate any help.

解决方案

Your payload isn't the required. Always use chrome's developers tab (networking) to outsource the real post/get informations

Use this payload

payload = {'loginName':user_field,
           'loginPwd':password_field,
           'btnLogin':''}

Your dict looks like this:

payload = {'user_field':user_field,
           'pass_field':pass_field}

The dict's keys don't have the same name of the real data, and you missed the btnLogin field. Sadly with any false information, the webserver will trash your request, because it has a specific "hard-coded" values which have to passed.

这篇关于Python请求登录 - 登录页面没有错误返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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