Python3-如何使用隐藏值登录到web表单?

Python3-如何使用隐藏值登录到web表单?,python,python-3.x,python-requests,python-3.4,Python,Python 3.x,Python Requests,Python 3.4,我正在尝试编写一个python脚本来登录以下站点,以便自动关注我们的一些商户帐户详细信息: 我使用的凭据是只读的,因此不能用于任何邪恶的东西,但有些东西工作不正常 到目前为止,我的代码是: import urllib from requests import session LOGIN_URL = "https://secure.worldpay.com/sso/public/auth/login.html?serviceIdentifier=merchantadmin" _page =

我正在尝试编写一个python脚本来登录以下站点,以便自动关注我们的一些商户帐户详细信息:

我使用的凭据是只读的,因此不能用于任何邪恶的东西,但有些东西工作不正常

到目前为止,我的代码是:

import urllib
from requests import session

LOGIN_URL = "https://secure.worldpay.com/sso/public/auth/login.html?serviceIdentifier=merchantadmin"

_page = urllib.urlopen(LOGIN_URL)

_contents = _page.read()

_jlbz_index = _contents.find("jlbz")
_jlbz_start_index = _jlbz_index + 5
_jlbz_end_index = _jlbz_start_index + 41
jlbz = _contents[_jlbz_start_index:_jlbz_end_index]

fdt = _contents.find("formDisplayTime")
fdt_start_index = fdt + 23
fdt_end_index = fdt_start_index + 13
form_display_time = _contents[fdt_start_index:fdt_end_index]

fsh = _contents.find("formSubmitHash")
fsh_start_index = fsh + 22
fsh_end_index = fsh_start_index + 41
form_submit_hash = _contents[fsh_start_index:fsh_end_index]

post_auth_url = "https://secure-test.worldpay.com/merchant/common/start.html?jlbz={0}".format(jlbz)

payload = {
    "action": "j_security_check",
    "username": "USERNAME",
    "password": "PASSWORD",
    "jlbz": jlbz,
    "maiversion": "version1",
    "formDisplayTime": form_display_time,
    "formSubmitHash": form_submit_hash
}

with session() as c:
    c.post(LOGIN_URL, data=payload)
    request = c.get(post_auth_url)
    print(request.headers)
    print(request.text)
我知道它现在有点冗长,但我发现在第一次尝试某样东西,然后在以后改进时,写得稍微详细一点会更容易

jlbz、formDisplayTime和formSubmitHash都是来自页面源的隐藏输入值-我正在从页面中删除这些值,但很明显,当我到达c.post时,我再次打开URL,所以这些值正在更改并且不再有效?但是,我不确定如何重写c.post行以确保提取正确的隐藏值以提交

我不认为这仅仅与这个站点有关,但是对于任何具有隐藏随机值的站点

import requests
from bs4 import BeautifulSoup

user='xyzmohsin'
passwd='abcpasswd'

s=requests.Session()
headers={"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"}
s.headers.update(headers)
r=s.get("https://secure.worldpay.com/sso/public/auth/login.html?serviceIdentifier=merchantadmin")
soup=BeautifulSoup(r.content)
jlbz=soup.find("input",{"name":"jlbz"})['value']
maiversion=soup.find(id="maiversion")['value']
formDisplayTime=soup.find("input",{"name":"formDisplayTime"})['value']
formSubmitHash=soup.find("input",{"name":"formSubmitHash"})['value']

data={"jlbz":jlbz,
"username":user,
"password":passwd,
"maiversion":maiversion,
"formDisplayTime":formDisplayTime,
"formSubmitHash":formSubmitHash}

headers={"Content-Type":"application/x-www-form-urlencoded",
"Host":"secure.worldpay.com",
"Origin":"https://secure.worldpay.com",
"Referer":"https://secure.worldpay.com/sso/public/auth/login.html?serviceIdentifier=merchantadmin"}

login_url="https://secure.worldpay.com/sso/public/auth/j_security_check"

r=s.post(login_url,headers=headers,data=data)
我没有ID和密码,因此我不知道哪些头可以工作。 但如果这不起作用,请从最后一个
s.post
请求的标题中删除
主机、源站和引用方


希望有帮助:-)

只是一件事,他们可能会使用您的用户代理来散列请求,因此urllib有两个不同的用户代理,从而创建不同的散列。另外,由于他们有formDisplayTime,你也应该尝试使用一点睡眠,因为他们可能会计算你看了多长时间来刮掉愚蠢的机器人。当我输入正确的凭据并调用post_auth URL时,这就产生了奇迹。非常感谢!非常感谢。使用源url和主机值设置标题修复了我遇到的问题(Apache tomcat“HTTP状态408-已超过登录过程允许的时间”)