python通过urllib2提交http post请求的代码

1,201 阅读1分钟
下面的资料是关于python通过urllib2提交http post请求的内容,应该能对各位有较大好处。 

#!/usr/bin/python  
#coding=utf-8  
  
import urllib  
import urllib2  
  
def post(url, data):  
    req = urllib2.Request(url)  
    data = urllib.urlencode(data)  
    #enable cookie  
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())  
    response = opener.open(req, data)  
    return response.read()  
  
def main():  
    data = {'email':'myemail', 'password':'mypass', 'autologin':'1', 'submit':'登 录', 'type':''}  
    print post(posturl, data)  
  
if __name__ == '__main__':  
    main()