Python simulated login 163 mailbox and get communication:
#-*-Coding: UTF-8 -*-
Import urllib, urllib2, cookielib
Import XML. etree. elementtree as etree
# XML Parsing
Class login163:
# Camouflage Browser
Header = {'user-agent': 'mozilla/5.0 (windows; U; Windows NT 6.1; en-US; RV: 1.9.1.6) Gecko/20091201 Firefox/3.5.6 '}
Username =''
Passwd =''
Cookie = none # cookie object
Cookiefile = './cookies. dat'
# Temporary cookie storage location
User =''
Def _ init _ (self, username, passwd ):
Self. Username = Username
Self. passwd = passwd
# Cookie settings
Self. Cookie = cookielib. lwpcookiejar ()
# Storing custom cookies
Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (self. Cookie ))
Urllib2.install _ opener (opener)
# Login
Def login (Self ):
# Request parameter settings
Postdata = {
'Username': Self. username,
'Password': Self. passwd,
'Type': 1
}
Postdata = urllib. urlencode (postdata)
# Initiate a request
Req = urllib2.request (
Url = 'HTTP: // reg.163.com/logins.jsp? Type = 1 & Product = mail163 & url = http://entry.mail.163.com/coremail/fcg/ntesdoor2? Lightweight % 3d1% 26 verifycookie % 3d1% 26 Language % 3d-1% 26 style % 3d1 ',
Data = postdata, # request data
Headers = self. Header
# Request Header
)
Result = urllib2.urlopen (req). Read ()
Result = STR (result)
Self. User = self. username. Split ('@') [0]
Self. Cookie. Save (self. cookiefile) # Save cookie
If 'login successful, jump... 'in result:
# Print ("% s you have successfully logged on to the 163 mailbox. --------- \ N "% (User ))
Flag = true
Else:
Flag = '% s failed to log on to the 163 mailbox. '% (Self. User)
Return flag
# Getting address book
Def address_list (Self ):
# Obtain the authentication Sid
Auth = urllib2.request (
Url = 'HTTP: // entry.mail.163.com/coremail/fcg/ntesdoor2? Username = '+ self. User +' & lightweight = 1 & verifycookie = 1 & language =-1 & Style = 1 ',
Headers = self. Header
)
Auth = urllib2.urlopen (auth). Read ()
For I, Sid in enumerate (self. Cookie ):
SID = STR (SID)
If 'sid 'in Sid:
SID = Sid. Split () [1]. Split ('=') [1]
Break
Self. Cookie. Save (self. cookiefile)
# Request address
Url = 'HTTP: // twebmail.mail.163.com/js4/s? SID = '+ Sid +' & func = Global: sequential & showad = false & usertype = browser & uid = '+ self. Username
# Parameter setting (the VaR variable is required, otherwise you can only see the following information: <code> s_ OK </code> <messages/>)
# Here, the parameters are also viewed in firebug.
Postdata = {
'Function': 'Global: sequential ',
'Showad': 'false ',
'Sid': 'qacvwiwofuumhpdcyqooutajexnbbear ',
'Uid': Self. username,
'Usertype': 'browser ',
'Var': '<? XML version = "1.0"?> <Object> <array name = "items"> <Object> <string name = "func"> PAB: searchcontacts </string> <object name = "Var"> <array name = "order"> <Object> <string name = "field"> FN </string> <Boolean name = "DESC"> false </Boolean> <Boolean
Name = "ignorecase"> true </Boolean> </Object> </array> </Object> <string name = "func"> PAB: getallgroups </string> </Object> </array> </Object>'
}
Postdata = urllib. urlencode (postdata)
# Assembly request
Req = urllib2.request (
Url = URL,
Data = postdata,
Headers = self. Header
)
Res = urllib2.urlopen (req). Read ()
# Parse XML and convert it to JSON
# Note: Because XML format data is provided in 163 after such a request,
# It is best to convert the returned data to JSON for convenience.
JSON = []
Tree = etree. fromstring (RES)
OBJ = none
For child in tree:
If child. Tag = 'array ':
OBJ = Child
Break
# For more information, see the method attributes of the etree element, including attrib, text, tag, and getchildren ().
OBJ = OBJ [0]. getchildren (). Pop ()
For child in OBJ:
For X in child:
ATTR = x. attrib
If ATTR ['name'] = 'email; pref ':
Value = {'email ': X. Text}
JSON. append (value)
Return JSON
# Demo
Print ("requesting... \ n ")
Login = login163 ('xxxx @ 163.com ', 'xxxxx ')
Flag = login. login ()
If type (FLAG) is bool:
Print ("successful landing, resolved contacts... \ n ")
Res = login. address_list ()
For X in Res:
Print (X ['email '])
Else:
Print (FLAG)