Urllib2: Submitting A Form And Then Redirecting
Solution 1:
The standard way to follow redirects with urllib2 is to use the HTTPRedirectHandler. (Not sure what you mean by 'what comes out' but I'm assuming a standard http redirect here, javascript redirect is a different beast)
# Created handler
redirectionHandler = urllib2.HTTPRedirectHandler()
# 2 apply the handler to an opener
opener = urllib2.build_opener(redirectionHandler)
# 3. Install the openers
urllib2.install_opener(opener)
request = urllib2.Request('https://some.site/page', data=urllib.urlencode({'key':'value'}))
response = urllib2.urlopen(request)
See urllib2.HTTPRedirectHandler for details on the handler.
Solution 2:
I suspect this will almost always fail. When you POST a form, the URL you end up at is just the URL you posted to. Sending someone else to this URL, or even visiting it again with the same browser that just POSTed, is going to do a GET and the page won't have the form data that was POSTed. The only way this would work is if the site redirected after the POST to a URL containing some sort of session info.
Solution 3:
You will find using mechanize much easier than using urllib2 directly
Post a Comment for "Urllib2: Submitting A Form And Then Redirecting"