Friday, August 14, 2009

Python: Perform HTTP POST operation with urllib2

The request function under the urllib2 class accepts both url and parameter. If you pass only url, it will act as HTTP GET. But if you pass the parameter with the url, it will perform the HTTP POST operation, where the url will be your post url, and the parameter will be http post content.


import urllib2
req = urllib2.Request(url, param)
res = urllib2.urlopen(req)
html = decodeContent(res)
res.close()


You can use the urllib2 various way to get HTML content from the website. I'm giving the example which I like to use. It will be different if you want to use it other ways.

1 comment:

Anonymous said...

What is decode()?

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...