无法使用 python 个请求 POST 数据

couldn't POST data using python requests

我正在尝试使用 python post 请求访问此 email-verifier 端。但是我收到 Bad request 错误。下面一个是网站的输入表单

<form method="post" action="/" novalidate="novalidate">
<input type="hidden" id="dpf" name="DFP" value="W3sia2V5IjoidXNlckFnZW50IiwidmFsdWUiOiJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvODguMC40MzI0LjE5MCBTYWZhcmkvNTM3LjM2In0seyJrZXkiOiJ3ZWJkcml2ZXIiLCJ2YWx1ZSI6Im5vdCBhdmFpbGFibGUifSx7ImtleSI6Imxhbmd1YWdlIiwidmFsdWUiOiJlbi1DQSJ9LHsia2V5IjoiY29sb3JEZXB0aCIsInZhbHVlIjoyNH0seyJrZXkiOiJkZXZpY2VNZW1vcnkiLCJ2YWx1ZSI6OH0seyJrZXkiOiJwaXhlbFJhdGlvIiwidmFsdWUiOjEuMjV9LHsia2V5IjoiaGFyZHdhcmVDb25jdXJyZW5jeSIsInZhbHVlIjo0fSx7ImtleSI6InRpbWV6b25lT2Zmc2V0IiwidmFsdWUiOi0zMzB9LHsia2V5IjoidGltZXpvbmUiLCJ2YWx1ZSI6IkFzaWEvQ29sb21ibyJ9LHsia2V5Ijoic2Vzc2lvblN0b3JhZ2UiLCJ2YWx1ZSI6dHJ1ZX0seyJrZXkiOiJsb2NhbFN0b3JhZ2UiLCJ2YWx1ZSI6dHJ1ZX0seyJrZXkiOiJpbmRleGVkRGIiLCJ2YWx1ZSI6dHJ1ZX0seyJrZXkiOiJhZGRCZWhhdmlvciIsInZhbHVlIjpmYWxzZX0seyJrZXkiOiJvcGVuRGF0YWJhc2UiLCJ2YWx1ZSI6dHJ1ZX0seyJrZXkiOiJjcHVDbGFzcyIsInZhbHVlIjoibm90IGF2YWlsYWJsZSJ9LHsia2V5IjoicGxhdGZvcm0iLCJ2YW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ZlOGQ5YWQzOTc1NTZhZjhlO3ZpZGVvaW5wdXQ7IiwiaWQ9O2dpZD02OGVjNGNmZTQ1MzJiZjExY2E0ZWI4OTM1NzYxZjI5OTRjZTAxODA0N2ZhYjFmYTEyNzc2MGQxNmUzZTAzODdjO2F1ZGlvb3V0cHV0OyJdfV0=">
<input type="hidden" id="aba" data-val="true" data-val-required="The aba field is required." name="ABA" value="false">
<div class="card">
<div class="card-header">
<div>
<div class="float-left">
<h3 class="card-title"><i class="fal fa-bars"></i>&nbsp;Verify Email Address In Real-Time</h3>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="card-body">
<div class="input-group input-group-lg">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-envelope-open"></i></span>
</div>
<input id="input-email-address" class="form-control" autocomplete="off" placeholder="email to verify" type="email" data-val="true" data-val-email="The Email address field is not a valid e-mail address." data-val-maxlength="The field Email address must be a string or array type with a maximum length of '255'." data-val-maxlength-max="255" data-val-minlength="The field Email address must be a string or array type with a minimum length of '3'." data-val-minlength-min="3" data-val-required="The Email address field is required." maxlength="255" name="EmailAddress" value="lavan@gmail.cooom">
<div class="input-group-append">
<button class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="" formaction="/" data-original-title="Verify an email address now">go</button>
</div>
</div>
</div>
<div class="card-footer">
<div class="row">
<div class="col">
<i class="fa fa-stethoscope"></i>&nbsp;<span class="alert-danger field-validation-valid" data-valmsg-for="EmailAddress" data-valmsg-replace="true"></span><span class="alert-danger field-validation-valid" data-valmsg-for="ErrorMessage" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</div>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8Dn81EZjq0pAmp3kd_YLe05aZfYqkFZuq0SbqZ5B_PZf7vH4ZDwPl4WEnu2kDiS4m2cWUaY7VoP_1V6WRUdw-sKN9rw6NiEaThdvg73srss-Ozbcl27aoi5TcNtL8lj6M-tLT_lkFtc1gLbHxsGuHVY"></form>

这是我到目前为止尝试过的...

import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1MjUxZTIwNzMwNGEwYTlhMDEzNTE1ODU4OTM1YWIzODgxZjY3YWU1O2F1ZGlvb3V0cHV0OyJdfV0=',
        'ABA': 'false',
        'EmailAddress':'HERE I GAVE THE EMAIL ADDRESS TO VERIFY',
        '__RequestVerificationToken': 'CfDJ8Dn81EZjq0pAmp3kd_YLe049yQv3aB1-CkQbIlD_4vXIesj6Uz9Jmbjqi1b8U99T9oSnyZZhnd37VzRlNXs7TCLIQ7qQCiFVWgaENNPcL8faDvd32TMC7y-kp92NNj8I-Z_EV0PcB5oUbIgwmhdrSdw'


        }
r = requests.post(url,data)
r.status_code # this is returning 400

我做错了什么?

你可以使用这个方法:

from bs4 import BeautifulSoup
import requests


url = "https://localhost:5001/Account/Login"

session=requests.session()

bfs= BeautifulSoup(session.get(url).content,'html.parser')
csrftoken = bfs.find('input', dict(name='__RequestVerificationToken'))['value']
dfp= bfs.find('input',dict(name="DFP"))["value"]
parameters={
    "__RequestVerificationToken":csrftoken,
    "DFP": dfp,
    "Username":"user",
    "Password":"pass",
}
respons=session.post(url=url,data=parameters)
print(respons.text)

那我们做了什么?

你看,我们必须先创建一个会话 并使用 BeautifulSoup 我可以加载一次页面并拉出 (__RequestVerificationToken,DFP,..) 并使用我们打开的同一会话发送我们的请求。