Python模拟163登陆获取邮件列表(1)


利用cookielib和urllib2模块模拟登陆163的例子有很多,近期看了《python模拟登陆163邮箱并获取通讯录》一文,受到启发,试着对收件箱、发件箱等进行了分析,并列出了所有邮件列表及状态,包括发件人、收件人、主题、发信时间、已读未读等状态。

1、参考代码http://hi.baidu.com/fc_lamp/blog/item/2466d1096fcc532de8248839.html%EF%BB%BF

  1. #-*- coding:UTF-8 -*-  
  2. import urllib,urllib2,cookielib  
  3. import xml.etree.ElementTree as etree #xml解析类  
  4.  
  5. class Login163:  
  6.    #伪装browser  
  7.     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'}  
  8.     username = '' 
  9.     passwd = '' 
  10.     cookie = None #cookie对象  
  11.     cookiefile = './cookies.dat' #cookie临时存放地  
  12.     user = '' 
  13.       
  14.     def __init__(self,username,passwd):  
  15.         self.username = username  
  16.         self.passwd = passwd  
  17.         #cookie设置  
  18.         self.cookie = cookielib.LWPCookieJar() #自定义cookie存放  
  19.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))  
  20.         urllib2.install_opener(opener)  
  21.  
  22.    #登陆      
  23.     def login(self):         
  24.  
  25.         #请求参数设置  
  26.         postdata = {  
  27.             'username':self.username,  
  28.             'password':self.passwd,  
  29.             'type':1 
  30.             }  
  31.         postdata = urllib.urlencode(postdata)  
  32.  
  33.         #发起请求  
  34.         req = urllib2.Request(  
  35.                 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',  
  36.                 data= postdata,#请求数据  
  37.                 headers = self.header #请求头  
  38.             )  
  39.  
  40.         result = urllib2.urlopen(req).read()  
  41.         result = str(result)  
  42.         self.user = self.username.split('@')[0]  
  43.  
  44.         self.cookie.save(self.cookiefile)#保存cookie  
  45.           
  46.         if '登录成功,正在跳转...' in result:  
  47.             #print("%s 你已成功登陆163邮箱。---------\n" %(user))  
  48.             flag = True 
  49.         else:  
  50.             flag = '%s 登陆163邮箱失败。'%(self.user)  
  51.              
  52.         return flag  
  53.  
  54.    #获取通讯录  
  55.     def address_list(self):  
  56.  
  57.         #获取认证sid  
  58.         auth = urllib2.Request(  
  59.                 url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?username='+self.user+'&lightweight=1&verifycookie=1&language=-1&style=1',  
  60.                 headers = self.header  
  61.             )  
  62.         auth = urllib2.urlopen(auth).read()  
  63.         for i,sid in enumerate(self.cookie):#enumerate()用于同时返数字索引与数值,实际上是一个元组:((0,test[0]),(1,test[1]).......)这有点像php里的foreach 语句的作用  
  64.             sid = str(sid)  
  65.             if 'sid' in sid:  
  66.                 sid = sid.split()[1].split('=')[1]  
  67.                 break 
  68.         self.cookie.save(self.cookiefile)  
  69.           
  70.         #请求地址  
  71.         url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username  
  72.         #参数设定(var 变量是必需要的,不然就只能看到:<code>S_OK</code><messages/>这类信息)  
  73.         #这里参数也是在firebug下查看的。  
  74.         postdata = {  
  75.             'func':'global:sequential',  
  76.             'showAd':'false',  
  77.             'sid':sid,  
  78.             'uid':self.username,  
  79.             'userType':'browser',  
  80.             '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>' 
  81.             }  
  82.         postdata = urllib.urlencode(postdata)  
  83.           
  84.         #组装请求  
  85.         req = urllib2.Request(  
  86.             url = url,  
  87.             data = postdata,  
  88.             headers = self.header  
  89.             )  
  90.         res = urllib2.urlopen(req).read()  
  91.           
  92.         #解析XML,转换成json  
  93.         #说明:由于这样请求后163给出的是xml格式的数据,  
  94.         #为了返回的数据能方便使用最好是转为JSON  
  95.         json = []  
  96.         tree = etree.fromstring(res)  
  97.         obj = None 
  98.         for child in tree:  
  99.             if child.tag == 'array':  
  100.                 obj = child              
  101.                 break 
  102.         #这里多参考一下,etree元素的方法属性等,包括attrib,text,tag,getchildren()等  
  103.         obj = obj[0].getchildren().pop()  
  104.         for child in obj:  
  105.             for x in child:  
  106.                 attr = x.attrib  
  107.                 if attr['name']== 'EMAIL;PREF':  
  108.                     value = {'email':x.text}  
  109.                     json.append(value)  
  110.         return json  
  111.           
  112. #Demo  
  113. print("Requesting......\n\n")  
  114. login = Login163('xxxx@163.com','xxxxx')  
  115. flag = login.login()  
  116. if type(flag) is bool:  
  117.     print("Successful landing,Resolved contacts......\n\n")  
  118.     res = login.address_list()  
  119.     for x in res:  
  120.         print(x['email'])  
  121. else:  
  122.     print(flag) 


评论关闭