Python机械化307 E

2024-04-25 19:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在两个不同的页面上填写表单,第二个页面的表单操作是一个.cgi链接。我完全可以很好地填写这两个表单,但是当我尝试提交第二个表单时,mechanize返回一个307错误,这是由服务器生成的。在

我查看了这个错误,它说这是一个临时的重定向错误,但我认为mechanize可以处理重定向?在

有问题的代码(我包括我试图访问的URL,因此代码可以运行):

from mechanize import *

counter = 0

br = Browser()
br.set_handle_redirect(HTTPRedirectHandler)
br.open('http://www.nationstates.net/page=create_nation1')
br.select_form('form')
br.form['name'] = 'faleraider' + str(counter)
br.form['currency'] = 'duobloon'
br.form['animal'] = 'parrot'
br.form['slogan'] = 'Arr, raise your new sails!'
br.form['email'] = 'falserraider' + str(counter) + '@sharklasers.com'
br.form['password'] = 'raider'
br.form['confirm_password'] = 'raider'
br.submit(name='create_nation')

br.select_form('form')
br.form['Q0'] = ['SD',]
br.form['Q1'] = ['SD',]
br.form['Q2'] = ['SD',]
br.form['Q3'] = ['SD',]
br.form['Q4'] = ['SD',]
br.form['Q5'] = ['SD',]
br.form['Q6'] = ['SD',]
br.form['Q7'] = ['SD',]
br.form.find_control('legal').selected=True
#br.set_debug_http(True)
response2 = br.submit() #error originates from here
print response2.get_data()

表单填写得很好,这是服务器返回的错误代码(我不知道服务器为什么返回错误代码,或者mechanize为什么不处理它)。注意,我添加了重定向处理程序,但没有帮助。在

表单发布到的页面是/cgi-bin/build_国家.cgi在

我尝试过使用urllib+urllib2、beautifulsoup和splitter,但是它们都返回了相似的结果,或者更糟。在


Tags: 代码frombrform服务器http表单错误