我正在尝试使用python请求填写网络表单。有下拉列表(选项列表)时,有人知道正确的语法吗?
我可以成功地发布到表单上的文本框,但不能成功发布到选项列表中
import requests
URL = 'http://127.0.0.1:8000/recoater/new/'
payload = {
'data': '40',
'machine': '"1">MachineA<',
}
r = requests.post(URL, data=payload)
print (r)
print(r.text)
返回此:
<ul class="errorlist"><li>Select a valid choice. That choice is not one of
the available choices.</li></ul>
<p><label for="id_machine">Machine:</label> <select name="machine" required
id="id_machine">
<option value="">---------</option>
<option value="1">MachineA</option>
答案 0 :(得分:3)
对于<select>..</select>
,POST
数据将发送所选value=".."
中的<option>
,因此,如果需要MachineA
,则应使用'machine': '1'
,作为有效载荷:
导入请求
URL = 'http://127.0.0.1:8000/recoater/new/'
payload = {
'data': '40',
'machine': '1',
}
r = requests.post(URL, data=payload)
print (r)
print(r.text)
Django表单(或其他处理请求的机制)具有将此值映射回与该值关联的计算机的逻辑:毕竟,选项中的文本只是文本表示(Machine
对象),它可以包含许多未显示(或以非结构化方式显示)的(额外)数据。
因此浏览器将浏览网页:
<select name="machine" required id="id_machine">
<option value="">---------</option>
<option value="1">MachineA</option>
</select>
在POST
数据中发送一个与该选项的name
关联的<select>
(此处为'machine'
)的value
关联的条目选择(此处为''
或'1'
)。就像<input name="data" type="text">
也有一个value
参数一样,该参数设置为您在文本字段中输入的值。