很久以来,我尝试了其他方法来使用python请求向表单提交POST请求,但是没有运气。我正在尝试将'3513'
发送到表单的文本字段,其名称为TextTrnNo
,然后从网站获取信息。
我失败的一种方法:
import requests
payload = {'TextTrnNo': '3513'}
r = requests.post('https://appiris.infofer.ro/mytrainro.aspx', data=payload)
print(r.text)
我得到的输出:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
IRIS Informatii tren
</title><link rel="shortcut icon" href="train.ico" type="image/x-icon" /><link rel="icon" href="train.ico" type="image/x-icon" />
<style type="text/css">
.style1
{
font-family: Tahoma;
font-size: small;
font-weight: bold;
color: #006699;
}
.style26
{
font-size: small;
font-family: Tahoma;
color: #003366;
}
.style28
{
font-family: Tahoma;
font-size: large;
font-weight: bold;
color: #006699;
}
.style29
{
width: 24%;
}
.style22
{
font-size: small;
font-weight: bold;
}
.style24
{
width: 162px;
color: #336699;
font-size: x-small;
background-color: #FFFFFF;
font-weight: bold;
height: 20px;
}
.style30
{
font-family: Tahoma;
font-size: small;
color: #006699;
}
.style17
{
color: #336699;
}
.style10
{
font-size: small;
font-family: Tahoma;
}
</style>
</head>
<body bgcolor="#aec7d2">
<form method="post" action="mytrainro.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTA5MDM3OTI0Nw9kFgICAw9kFgQCAw8PFgIeBFRleHQFGVNlcnZpY2lpIElSSVMgZGlzcG9uaWJpbGVkZAIJD2QWAmYPZBYQAgMPDxYCHwAFJjEzLjAxLjIwMTkgICAxNzowMCAtMTQuMDEuMjAxOSAgIDE3OjAwZGQCBQ8PFgIeB1Zpc2libGVoZGQCBw8PFgIfAWgWAh4Fc3R5bGUFZWNvbG9yOiMwMDAwMDA7Zm9udC1mYW1pbHk6J0NvbWljIFNhbnMgTVMnO2JhY2tncm91bmQtY29sb3I6I0ZGRkZGRjttYXJnaW4tbGVmdDowcHg7dGV4dC1hbGlnbjpjZW50ZXI7ZAIJDw8WAh8BaBYCHwIFaGZvbnQtZmFtaWx5OidDb21pYyBTYW5zIE1TJztmb250LXdlaWdodDo3MDA7Zm9udC1zaXplOm1lZGl1bTtiYWNrZ3JvdW5kLWNvbG9yOiNGRjMzMDA7dGV4dC1hbGlnbjpjZW50ZXI7ZAILDw8WAh8BaGRkAhMPPCsADwEKEBYAFgAWAGQCGQ88KwARAQEQFgAWABYAZAIbDzwrABEBARAWABYAFgBkGAMFCUdyaWRWaWV3Mg9nZAUJR3JpZFZpZXcxD2dkBQxEZXRhaWxzVmlldzEPZ2Q3+DY+/88Qx6xO2+RiB7wzTScNiSRFQ92R8m5GZzGbwg==" />
<script src="/ScriptResource.axd?d=JNg0fH_zydENJ7nFcOtvc6yqY25aZ8vD-Ng7zdFvgOejMomgDzr5SQOfJxd86HrHIAnJdwgVKGKwlCLMquTcg919u2vCLYJz5V0xHnmOduOfc5VgeUHRZPW3jweNO1vqw9JZLWTSVL3z295y_EUsB350MMAhbUa41ssYQzHWget11mAeRCd_ANONdJnWqlOV0&t=5854e822" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');
//]]>
</script>
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="86BE64DB" />
<p class="style28">
<img id="Image5" src="Img/icons/flagRO.jpg" />
CNCF "CFR SA" - IRIS Informatii tren</p>
<p class="style28">
<span class="style1">
<span id="LblError">Servicii IRIS disponibile</span>
</span>
</p>
<div>
<div id="UpdateProgress1" style="display:none;">
<span class="style26">
<img id="Image3" src="Images/Animate/pleasewait_7.gif" height="20" width="22" />
</span><span class="style1">Asteptati...</span>
</div>
<div id="UpdatePanel1">
<span class="style26"><span class="style1">
<img id="Image4" src="Images/tren.jpg" height="110" width="200" />
<br />
</span>
<span class="style30">Program circulatie</span>
<span class="style26"><span class="style17">
<span id="Label1" class="style10">13.01.2019 17:00 -14.01.2019 17:00</span>
<table class="style29">
<tr>
<td valign="bottom">
</td>
<td>
<span class="style26">
</span>
</td>
<td>
<span class="style26">
</span>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
Numar tren (fără alte caractere suplimentare)</span>
<input name="TextTrnNo" type="text" maxlength="15" id="TextTrnNo" title="Introduceti numai numărul trenului, fără alte caractere suplimentare" />
<input type="submit" name="Button1" value="Informatii tren" id="Button1" style="font-family: Tahoma; font-size: small; font-weight: 700; color: #006699; background-color: #FFFFFF" />
<span id="Lblx" style="font-family: Tahoma; font-size: small; color: #CC0000"><b><font size="5"></font></b></span>
<br />
<br>
<div>
</div>
<div>
</div>
<div>
</div>
<br />
<br />
</div>
</div>
</form>
</body>
</html>
来自谷歌浏览器的标题:
Request URL: https://appiris.infofer.ro/ScriptResource.axd?d=JNg0fH_zydENJ7nFcOtvc6yqY25aZ8vD-Ng7zdFvgOejMomgDzr5SQOfJxd86HrHIAnJdwgVKGKwlCLMquTcg919u2vCLYJz5V0xHnmOduOfc5VgeUHRZPW3jweNO1vqw9JZLWTSVL3z295y_EUsB350MMAhbUa41ssYQzHWget11mAeRCd_ANONdJnWqlOV0&t=5854e822
Request Method: GET
Status Code: 200 (from memory cache)
Remote Address: 193.230.156.188:443
Referrer Policy: no-referrer-when-downgrade
cache-control: public
content-encoding: gzip
content-length: 55951
content-type: application/x-javascript
date: Sun, 13 Jan 2019 17:04:19 GMT
expires: Sun, 12 Jan 2020 22:49:44 GMT
last-modified: Sat, 12 Jan 2019 22:49:44 GMT
server: Microsoft-IIS/6.0
status: 200
x-powered-by: ASP.NET
Provisional headers are shown
d: JNg0fH_zydENJ7nFcOtvc6yqY25aZ8vD-Ng7zdFvgOejMomgDzr5SQOfJxd86HrHIAnJdwgVKGKwlCLMquTcg919u2vCLYJz5V0xHnmOduOfc5VgeUHRZPW3jweNO1vqw9JZLWTSVL3z295y_EUsB350MMAhbUa41ssYQzHWget11mAeRCd_ANONdJnWqlOV0
t: 5854e822
答案 0 :(得分:2)
您的代码正确无误,就像它发送带有所需数据的发件请求一样。但是,在查看发送到站点的网络请求时,我注意到您缺少几个字段,这些字段阻止站点解析发布请求。这些字段是:
Button1
__VIEWSTATE
它们可以完全为空,它们只需要作为发布请求的一部分,从而使您的完整有效负载变量:
payload = {'TextTrnNo': '3513', 'Button1': '', '__VIEWSTATE': ''}
这将返回不同的输出,其中包括某种表。