您的位置:首页 > 编程语言 > Python开发

python实现自动登录

2016-11-09 10:25 573 查看
最近大数据感兴趣实践了一下自动登录,返回结果是登录成功


import urllib
import urllib2

import gzip
import StringIO

#构建post请求的字典
postvalues={"username":"xxxxx","password":"xxxxx","formhash":"xxxxx","referer":"xxxxxx","questionid":"0","loginsubmit":"true"}
postdata = urllib.urlencode(postvalues);

#构建get请求的字典
getvalues={}
getvalues['mod']="logging"
getvalues['action']="login"
getvalues['loginsubmit']="yes"
getvalues['loginhash']="xxxxxx"
getvalues['inajax']="1"

getdata=urllib.urlencode(getvalues)
#拼接get请求
urlsrc="xxxxxxxx"+"?"+getdata

headers={'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Content-Type':'application/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7',
'Referer':'',
'Host':'xxxxxx'}

#传入url,postdata和header
request = urllib2.Request(urlsrc,postdata,headers)

response = urllib2.urlopen(request).read()
#解压返回值
data =  StringIO.StringIO(response)
gzipper = gzip.GzipFile(fileobj=data)

html=gzipper.read()
#返回中有部分乱码,是编码问题,从返回网页结果看是gbk编码
print html.decode("gbk")
print  response.read()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: