Python 请求 - 在 FORM_DATA 中插入凭据作为变量 [英] Python Requests - Inserting credentials as variables in FORM_DATA

查看:25
本文介绍了Python 请求 - 在 FORM_DATA 中插入凭据作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提示用户输入用户名和密码以登录站点.我的计划是使用请求来发送 POST 数据.这是我到目前为止的代码:

I'm trying to prompt the user for the username and password to login to a site. My plan is to use requests to send the POST data. Here is the code I have so far:

import requests
import getpass

uname = raw_input("Username: ")
passw = getpass.getpass(prompt = "Password: ")

FORM_DATA = {
"__EVENTTARGET:",
"__EVENTARGUMENT:",
"__VIEWSTATE:/wEPDwUKMTA5NTA5ODU1MQ9kFgJmD2QWAgIGDxBkDxYFZgIBAgICAwIEF***REMAINDER REMOVED***",
"__EVENTVALIDATION:/wEdAAp4d3BHvSTs+Kv6cxGP3xEbBr8xrgRYad2tj4YCyRIw5qUAjimf****REMAINDER REMOVED****",
"jsCheck:",
"ddlEngine: REMOVED:13008",
"Username: %(uname)s" ,
"Password: %(passw)s",
"btnLogin.x: 42",
"btnLogin.y: 9",
"btnLogin: Login",
}

print FORM_DATA

这显然只是 POST 数据的一部分,但我想在继续之前先验证凭据是否已通过.使用 print 不会显示输入已插入到定义的点中.

This is obviously just a portion of the POST data, but I want to verify the credentials are passed first before moving forward. Using print does not show that the input has been inserted into the defined spots.

推荐答案

来自表单的 POST 数据只是一系列键值对.因为所有的键都是唯一的,就用字典来表示这些:

POST data from a form is just a series of key-value pairs. Because all the keys are unique, just use a dictionary to represent these:

FORM_DATA = {
    '__EVENTTARGET': '',
    '__EVENTARGUMENT': '',
    '__VIEWSTATE': '/wEPDwUKMTA5NTA5ODU1MQ9kFgJmD2QWAgIGDxBkDxYFZgIBAgICAwIEF***REMAINDER REMOVED***',
    '__EVENTVALIDATION': '/wEdAAp4d3BHvSTs+Kv6cxGP3xEbBr8xrgRYad2tj4YCyRIw5qUAjimf****REMAINDER REMOVED****',
    'jsCheck': '',
    'ddlEngine': 'REMOVED:13008',
    'Username': uname,
    'Password': passw,
    'btnLogin.x': '42',
    'btnLogin.y': '9',
    'btnLogin': 'Login',
}

用空字符串表示空值.

btnLogin.xbtnLogin.y通常只是被服务器忽略;它们会传达您单击图像按钮的位置.您可以直接从您已经设置的 unamepassw 变量中设置用户名和密码密钥.

The btnLogin.x and btnLogin.y keys are usually just ignored by a server; they communicate where you clicked on an image button. You can just set the username and password keys directly from the uname and passw variables you already set.

然后使用这个字典作为 requests.post() 调用的 data 关键字.

Then use this dictionary as the data keyword to the requests.post() call.

可能是服务器期望返回 cookie,从表单的初始加载设置.在这种情况下,会话对象并调用 .get().post() .

It may be the server expects a cookie back, set from the initial loading of the form. In that case a Session object and call .get() and .post() on that.

可能__VIEWSTATE__EVENTVALIDATION 键在表单第一次生成时动态设置,您需要加载它requests.get(),在发送之前解析 HTML(例如使用 BeautifulSoup)并提取表单字段.

It may be that the __VIEWSTATE and __EVENTVALIDATION keys are dynamically set when the form is first generated, you would need to load it with requests.get(), parse the HTML (using, say, BeautifulSoup) and extract the form fields before sending.

这篇关于Python 请求 - 在 FORM_DATA 中插入凭据作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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