Python' Mechanize submission HTTP 404 error
I want to make a python script using mechanize to log in as a specific user. Go to a specific page on that site, and fill out submit a form. I have successfully logged in, went to that page, filled out the form, but when I submit I get a weird error.
import mechanize
#from mechanize import ParseResponse, urlopen, urljoin
br=mechanize.Browser()
cj=mechanize.CookieJar()
br.set_cookiejar(cj)
br.set_handle_robots(False)
request=mechanize.Request("http://www.reddit.com/")
response=br.open("http://www.reddit.com/")
cj.extract_cookies(response, request)
request2=mechanize.Request("http://www.reddit.com/r/berkeley/about/flair/? name=bezzerkeley")
cj.add_cookie_header(request2)
br.select_form(nr=1)
br["user"]="bezzerkeley"
br["passwd"]="xxxxxxx"
response2=br.submit()
cj.extract_cookies(response2, request2)
response3=br.open("http://www.reddit.com/r/berkeley/about/flair/?name=bezzerkeley")
request3=mechanize.Request("http://www.reddit.com/r/berkeley/about/flair/")
cj.extract_cookies(response3, request3)
cj.add_cookie_header(request3)
br.select_form(nr=8)
br["text"]="BOT TEST COMPLETE"
I xxxxed out the password for logging in. After I run this code and I do a br.submit() I get a HTTP Error 404: Not Found. If I physically go and submit this form in this way, the browser redirects me to the same page with the submitted info.
Extra information: when I do a print br.form after running the above code I get:
(POST http://www.reddit.com/post/flair application/x-www-form-urlencoded
(HiddenControl(name=bezze开发者_高级运维rkeley) (readonly))
(TextControl(text=BOT TEST COMPLETE))
(TextControl(css_class=))
(SubmitButtonControl((None)=) (readonly)))
I can provide more information if needed. Anybody know why I'm getting this error?
精彩评论