简体   繁体   English

Python机械化307错误

[英]Python Mechanize 307 Error

I am trying to fill out forms on 2 different pages, with the second page's form's action being a .cgi link. 我正在尝试在2个不同的页面上填写表单,第二个页面的表单动作是.cgi链接。 I can fill out both forms absolutely fine, but when I try to submit the second form, mechanize returns a 307 error, which is generated from the server. 我可以完全填写两个表格,但是当我尝试提交第二个表格时,mechanize返回307错误,该错误是由服务器生成的。

I looked into the error, and it says it is a temporary redirect error, but I thought mechanize could handle redirects? 我调查了该错误,并说这是一个临时重定向错误,但我认为机械化可以处理重定向?

The code in question (I include the urls I am attempting to access so the code is runnable): 有问题的代码(我包含尝试访问的网址,因此代码可运行):

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()

The forms fill out fine, it is the error code the server is returning (I don't know why the server returns that as an error, or why mechanize is not handling it). 表格可以很好地填写,这是服务器返回的错误代码(我不知道为什么服务器将其作为错误返回,或者为什么机械化没有处理它)。 Note that I did add the redirect handler, it did not help. 请注意,我确实添加了重定向处理程序,但没有帮助。

The page the form posts to is /cgi-bin/build_nation.cgi 表单发布到的页面是/cgi-bin/build_nation.cgi

I have tried to use urllib + urllib2, Beautiful Soup, and splinter, but they all returned similar, or worse results. 我曾尝试使用urllib + urllib2,Beautiful Soup和splinter,但它们都返回了相似的结果,或更糟的结果。

After first submit, don't you think that you have to capture what is returned? 首次提交后,您是否不认为必须捕获返回的内容?
I have not used mechanize though! 我还没有使用机械化!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM