Python Simulation Login

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.