我想使用Python Mechanize来做这件事。 这是HTML页面:
<form action="config.php" method="POST">
<div>
<img src="/images/delete1.png" />
<strong>EmptyDir1</strong><br />
<input type="checkbox" value="3" name="manager[]" />
</div>
<div>
<img src="/images/delete2.png" />
<strong>EmptyDir2</strong><br />
<input type="checkbox" value="4" name="manager[]" />
</div>
<div>
<img src="/images/copy.png" />
<strong>CopyConf</strong><br />
<input type="checkbox" value="22" name="manager[]" />
</div>
................. and so on another 20
<div><input type="submit" value="Do Jobs!" /></div>
</form>
我有两个问题: 1.如何从此页面中选中所有复选框并提交? 2.如何选择除名称为“modify”的复选框以外的所有复选框? 代码示例将很棒。感谢
答案 0 :(得分:2)
每个复选框都使用此选项:
import mechanize
br = mechanize.Browser()
br.open(URL)
br.select_form(nr=0)
for i in range(0, len(br.find_control(type="checkbox").items)):
br.find_control(type="checkbox").items[i].selected =True
reponse = br.submit()
print reponse.read()
除了修改复选框之外的所有内容(尚未检查):
import mechanize
br = mechanize.Browser()
br.open(URL)
br.select_form(nr=0)
for i in range(0, len(br.find_control(type="checkbox").items)):
if "modify" not in str(br.find_control(type="checkbox").items[i]):
br.find_control(type="checkbox").items[i].selected =True
reponse = br.submit()
print reponse.read()