Go [Python] [Automatic login 163 e-mail]

Source: Internet
Author: User
Tags urlencode

#-*-Coding:utf-8-*-
Import Urllib,urllib2,cookielib
Import Xml.etree.ElementTree as Etree #xml解析类

Class Login163:
#伪装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对象
Cookiefile = './cookies.dat ' #cookie临时存放地
user = '

def __init__ (SELF,USERNAME,PASSWD):
Self.username = Username
SELF.PASSWD = passwd
#cookie设置
Self.cookie = Cookielib. Lwpcookiejar () #自定义cookie存放
Opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (Self.cookie))
Urllib2.install_opener (opener)

#登陆
def login (self):

#请求参数设置
PostData = {
' username ': self.username,
' Password ': self.passwd,
' Type ': 1
}
PostData = Urllib.urlencode (postdata)

#发起请求
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%26verifycookie%3d1%26language%3d-1%26style%3d1 ',
Data= PostData, #请求数据
headers = Self.header #请求头
)

result = Urllib2.urlopen (req). Read ()
result = STR (result)
Self.user = Self.username.split (' @ ') [0]

Self.cookie.save (Self.cookiefile) #保存cookie

If ' login is successful, jumping ... ' in result:
#print ("%s" You have successfully logged in to 163 mailbox.) ---------\ n "% (user))
Flag = True
Else
Flag = '%s ' failed to log on 163 mailbox. '% (Self.user)

Return flag

#获取通讯录
def address_list (self):

#获取认证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): #enumerate () is used to return both numeric indexes and numeric values, which is actually a tuple: ((0,test[0]), (1,test[1]) ... This is a bit like the role of a foreach statement in PHP.
Sid = Str (SID)
If ' Sid ' in SID:
Sid = Sid.split () [1].split (' = ') [1]
Break
Self.cookie.save (Self.cookiefile)

#请求地址
url = ' http://twebmail.mail.163.com/js4/s?sid= ' +sid+ ' &func=global:sequential&showad=false&usertype= Browser&uid= ' +self.username
#参数设定 (var variable is required, otherwise you can only see:<code>s_ok</code><messages/> this kind of information)
#这里参数也是在firebug下查看的.
PostData = {
' Func ': ' Global:sequential ',
' Showad ': ' false ',
' SID ': SID,
' 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></object><object><string Name= "func" >pab:getAllGroups</string></object></array></object> '
}
PostData = Urllib.urlencode (postdata)

#组装请求
req = Urllib2. Request (
url = URL,
data = PostData,
headers = Self.header
)
res = Urllib2.urlopen (req). Read ()

#解析XML, convert to JSON
#说明: Since 163 of such requests are given in XML-formatted data,
#为了返回的数据能方便使用最好是转为JSON
JSON = []
Tree = etree.fromstring (res)
obj = None
For child in Tree:
if Child.tag = = ' array ':
obj = Child
Break
#这里多参考一下, method attributes of etree elements, etc., including attrib,text,tag,getchildren (), etc.
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\n")
Login = Login163 (' [email protected] ', ' xxxxx ')
Flag = Login.login ()
If type (flag) is bool:
Print ("Successful landing,resolved contacts......\n\n")
res = Login.address_list ()
For X in Res:
Print (x[' email ')
Else
Print (flag)

Go [Python] [Automatic login 163 e-mail]

Related Article

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.