Import Urllib
Import Urllib.request
Import gzip
Import http
Import Http.cookiejar
#定义一个方法用于生成请求头信息, processing cookies
Def Getopener (head):
# Initialize a cookiejar to process cookies <pre name= "code" class= "Python" >
CJ = Http.cookiejar.CookieJar ()
#实例化一个opener
Pro = Urllib.request.HTTPCookieProcessor (CJ)
Opener = Urllib.request.build_opener (PRO)
Header = []
For key, value in Head.items ():
Elem = (key, value)
Header.append (Elem)
Opener.addheaders = Header
Return opener
def ungzip (data):
#定义一个方法来解压返回信息
Try: # trying to unzip
Print (' Extracting ... ')
data = gzip.decompress (data)
Print (' Unzip complete! ')
Except
Print (' Uncompressed, no decompression ')
Return data
#执行操作, just log in.
# header This represents a parameter encapsulated as a message header, and the parameter type is a dictionary
#postDict This is the dictionary parameter for the user name and password
def login (header,url,postdict):
Opener = Getopener (header)
PostData = Urllib.parse.urlencode (postdict). Encode ()
op = Opener.open (URL, postdata)
data = Op.read ()
data = ungzip (data)
Print (data)
if __name__ = = ' __main__ ':
#封装消息头, disguised as a browser
Header = {
' Host ': ' pcpre2.xjuke.com ',
' Connection ': ' Keep-alive ',
' Accept-language ': ' zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 ',
' Accept ': ' Application/json, Text/plain, */* ',
' User-agent ': ' mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) gecko/20100101 firefox/53.0 ',
' accept-encoding ': ' gzip, deflate ',
' Content-type ': ' Application/json;charset=utf-8 ',
' Referer ': ' http://pcpre2.xjuke.com/?c=q-5ce/',
' Content-length ': ' 58 '
}
#用户名和密码
Postdict = {
' username ': XXX,
' Password ': XX,
' Wechatcode ': ' Q-5ce '
}
Login (header, ' XXXXX ', postdict)
Python Simulation Login