python带cookie提交表单自动登录
1 import urllib 2 import urllib2 3 import cookielib 4 5 login_url = "xxxxxxxxxxxxx" 6 7 cj = cookielib.CookieJar()#实例化对象,创建cookie容器cj 8 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))#通过build_opener方法构造一个包含把cookie传给HTTP...构造方法的类实例的URL访问器 9 10 contens = opener.open(login_url).read()#这里是因为学校官网提交的表单中需要有一个lt参数,而它是变化的所以先获取解析一下得到它 11 lt = re.findall('<input type="hidden" name="lt" value="(.*)" />',contents1) 12
#payload 13 form_info={"username":"xx","password":"xx","lt":lt[0],"_eventId":"submit","errors":"0"} 14 15 postData = urllib.urlencode(form_info) 17 opener.addheaders=[("User-Agent","xxxx"),]#我是浏览器 18 19 resp = opener.open(login_url,postData) 20 21 print resp.read()