在mechanize表单中选择未命名文本字段(python)

3 投票
2 回答
3500 浏览
提问于 2025-04-16 10:33

我正在用Python和mechanize写一个程序,批量把街道地址转换成GPS坐标。这是我第一次使用mechanize。我可以在页面上选择表单(“form2”),但是表单里的文本框没有名字。我该怎么选择这个文本框,让mechanize能够输入我的文本呢?我试着通过它的ID来选择,但没有成功。

br.select_form("Form2") #works as far as i know
br.form["search"] = ["1 lakewood drive, christchurch"] #this is the field that i cannot select

这里是网站的源代码。

<form name="Form2" >
or  Type an <b>Address</b>
<input id="search" size="40" type="text" value=""  >
<input type="button" onClick="EnteredAddress();" value="Enter" />
</form>

任何帮助都非常感谢。

2 个回答

1

为了让你知道,我是通过使用lazy1的上面那个答案解决了这个问题,不过我当时是在使用find_control方法之后试图给一个值赋值。结果当然不行,因为赋值的问题。我深入研究了这个方法,发现了setattr(),这个方法非常适合用来给字段赋值。

这样做是行不通的

br.form.find_control(id="field id here") = "new value here"

这样做是可以的

br.form.find_control(id="field id here").__setattr__("value", "new value here")
3

这段代码的意思是:在一个表单中找到一个ID为“search”的控件。简单来说,就是在一个网页的表单里,寻找一个叫“search”的输入框或者按钮。

撰写回答