Python Mechanize向没有ID或名称的表单输入添加值

时间:2015-08-17 15:41:05

标签: python mechanize

之前使用过机械。尝试填写输入没有名称或ID的表单。他们只有一个班级。有没有办法按照你选择表格时可以做的数字来做到这一点?

<form class="go-action-form">

    <div class="go-action-form-no-auth">
        <input class="firstname" type="text" placeholder="First Name" data-required />
        <input class="lastname"  type="text" placeholder="Last Name"  data-required />
        <input class="email"     type="text" placeholder="Email"      data-required data-validation="email" />
        <input type="submit" class="submit-petition" id="submit-petition-embed-ub39aca073fec49a690c5f3acb4152aae" value="Submit My Name" />
    </div>
</form>

这就是我所拥有的。

br = mechanize.Browser()


br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)


br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)


url = "https://generationopportunity.org/petitions/free-the-food-trucks-in-raleigh/?utm_content=bufferfa496&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer"
br.open(url)

br.select_form(nr = 1)

br.form.set_value(firstname, nr=0)
br.form.set_value(lastname, nr=1)
br.form.set_value(email, nr=2)
br.form.set_value(zip, nr=3)


print br.form.controls[0]
print br.form.controls[1]
print br.form.controls[2]
print br.form.controls[3]

br.submit()

修改

我想出了如何在上面的内容中添加值。现在我无法弄清楚如何提交它。我试着说它是br.submit() - AttributeError:&#39; NoneType&#39;对象没有属性&#39;点击&#39;

由于

1 个答案:

答案 0 :(得分:1)

br.open(url)

# forms = [f for f in br.forms()]
# print forms[1]
# print forms[1].controls[0]

br.select_form(nr = 1)



br.form.set_all_readonly(False)


br.form.set_value(firstname, nr=0)
br.form.set_value(lastname, nr=1)
br.form.set_value(email, nr=2)
br.form.set_value(zip, nr=3)


br.submit()