使用(requests)python包提交表单后清空响应

问题描述 投票:0回答:1

我正在尝试使用(请求)模块提交表单。

这是我要提交的表单:

<form method="POST" enctype="multipart/form-data" action="/cgi-bin/claws72.pl">
<input type="hidden" name="email" value="[email protected]"><br>
<br>
Select tagset:
<input type="radio" name="tagset" value="c5" checked=""> C5
<input type="radio" name="tagset" value="c7"> C7
<br><br>
Select output style:
<input type="radio" name="style" value="horiz" checked=""> Horizontal
<input type="radio" name="style" value="vert"> Vertical
<input type="radio" name="style" value="xml"> Pseudo-XML
<br><br>
<textarea name="text" rows="10" cols="50" wrap="virtual">Type (or paste) your text to be tagged into this box.
</textarea>
<br>
<input type="SUBMIT" value="Tag text now">
<input type="reset" value="Reset form">
</form>

这是包含以下表格的网站:http://ucrel.lancs.ac.uk/claws/trial.html

这是我的代码:

import requests

data = {'email' : '[email protected]',
'tagset':'c7',
'style' : 'xml',
'text' : 'TEST' }

r = requests.post('http://ucrel.lancs.ac.uk/cgi-bin/claws72.pl', data=data)
print(r.text) #0 words tagged (Why? it should tag the (TEST) word)
print(r.ok) #True

我使用与我自己的网站形式相同的代码并且它有效,但无法弄清楚为什么它不会在这里这样做!您认为网站本身是否会阻止此类请求?

谢谢

python python-3.x python-requests
1个回答
1
投票

如果您使用Firebug或某些类似工具检查请求数据,您会看到请求数据实际上采用以下格式:

-----------------------------41184676334
Content-Disposition: form-data; name="email"

[email protected]
-----------------------------41184676334
Content-Disposition: form-data; name="tagset"

c7
-----------------------------41184676334
Content-Disposition: form-data; name="style"

xml
-----------------------------41184676334
Content-Disposition: form-data; name="text"

TEST
-----------------------------41184676334--

尝试以这种方式格式化数据,然后重试。此外,最好传递其他请求标头字段(例如Accept-EncodingContent-Type ......)。

© www.soinside.com 2019 - 2024. All rights reserved.