Python机械化 - 提交表单响应

时间:2015-04-19 16:20:31

标签: python forms web submit mechanize-python

我有这段代码:

import mechanize
def register():
            br = mechanize.Browser()
            br.set_handle_robots(False)
            br.open("http://somtehing.com/form2/index.php")
            br.select_form(nr=0)
            br.form["email"] = "stone@klikni.com"
            br.form["name"] = "Jan"
            br.form["nickname"] = "Vrsrly"
            br.form["postnum"] = "41182"
            br.form["age"] = "52"
            br.form["sex"] = ["man"]
            br.form["tel"] = "+420723545054"
            br.find_control("rules").items[0].selected = True
            res = br.submit()
            content = res.read()
            with open("mechanize_results.html", "w") as f:
                f.write(content)

但是当我查看mechanize_results.html网站时,它只是一个填充的表单,复选框已选中,但我不知道它是否已发送。当我手动完成时,它会将我重定向到一个页面,其中"已成功注册"写的。我没有得到机械化的回应。任何想法如何解决?

1 个答案:

答案 0 :(得分:0)

我刚刚解决了一个类似的问题,因为form.submit似乎没有效果

datat = {'username': 'lnIqd6Et','password':'4BafqzIm8sLgWxdvw9Cb'}
#Encode the parameters
data = urllib.urlencode(datat)
#Submit the form (POST request).
print "POST"
browser.open(post_url,data)
print "POSTED"
print browser.open(url_to_get).read()