自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python模擬163登陸獲取郵件列表

開發(fā) 后端
近期看了《python模擬登陸163郵箱并獲取通訊錄》一文,受到啟發(fā),試著對(duì)收件箱、發(fā)件箱等進(jìn)行了分析,并列出了所有郵件列表及狀態(tài),包括發(fā)件人、收件人、主題、發(fā)信時(shí)間、已讀未讀等狀態(tài)。

利用cookielib和urllib2模塊模擬登陸163的例子有很多,近期看了《python模擬登陸163郵箱并獲取通訊錄》一文,受到啟發(fā),試著對(duì)收件箱、發(fā)件箱等進(jìn)行了分析,并列出了所有郵件列表及狀態(tài),包括發(fā)件人、收件人、主題、發(fā)信時(shí)間、已讀未讀等狀態(tài)。

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對(duì)象  
  11.     cookiefile = './cookies.dat' #cookie臨時(shí)存放地  
  12.     user = '' 
  13.       
  14.     def __init__(self,username,passwd):  
  15.         self.username = username  
  16.         self.passwd = passwd  
  17.         #cookie設(shè)置  
  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.         #請(qǐng)求參數(shù)設(shè)置  
  26.         postdata = {  
  27.             'username':self.username,  
  28.             'password':self.passwd,  
  29.             'type':1 
  30.             }  
  31.         postdata = urllib.urlencode(postdata)  
  32.  
  33.         #發(fā)起請(qǐng)求  
  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,#請(qǐng)求數(shù)據(jù)  
  37.                 headers = self.header #請(qǐng)求頭  
  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 '登錄成功,正在跳轉(zhuǎn)...' 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.         #獲取認(rèn)證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()用于同時(shí)返數(shù)字索引與數(shù)值,實(shí)際上是一個(gè)元組:((0,test[0]),(1,test[1]).......)這有點(diǎn)像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.         #請(qǐng)求地址  
  71.         url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username  
  72.         #參數(shù)設(shè)定(var 變量是必需要的,不然就只能看到:<code>S_OK</code><messages/>這類信息)  
  73.         #這里參數(shù)也是在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.         #組裝請(qǐng)求  
  85.         req = urllib2.Request(  
  86.             url = url,  
  87.             data = postdata,  
  88.             headers = self.header  
  89.             )  
  90.         res = urllib2.urlopen(req).read()  
  91.           
  92.         #解析XML,轉(zhuǎn)換成json  
  93.         #說明:由于這樣請(qǐng)求后163給出的是xml格式的數(shù)據(jù),  
  94.         #為了返回的數(shù)據(jù)能方便使用最好是轉(zhuǎn)為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) 

#p#

2、分析收件箱、發(fā)件箱等網(wǎng)址

在參考代碼中,獲取通訊錄的url為

url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username,通過對(duì)郵箱地址的分析,發(fā)現(xiàn)收件箱、發(fā)件箱等的url為url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=mbox:listMessages&showAd=false&userType=browser&uid='+self.username,其中func=mbox:listMessages。其對(duì)收件箱、發(fā)件箱的具體區(qū)分在下面的postdata中,具體為:

(1)收件箱

  1. postdata = {  
  2. 'func':'global:sequential',  
  3. 'showAd':'false',  
  4. 'sid':'qACVwiwOfuumHPdcYqOOUTAjEXNbBeAr',  
  5. 'uid':self.username,  
  6. 'userType':'browser',  
  7. 'var':'<!--?xml version="1.0"?--><object><int name="fid">1</int><string name="order">date</string><boolean name="desc">true</boolean><boolean name="topFirst">false</boolean><int name="start">0</int><int name="limit">20</int></object>' 

(2)發(fā)件箱

  1. postdata = {  
  2. 'func':'global:sequential',  
  3. 'showAd':'false',  
  4. 'sid':'qACVwiwOfuumHPdcYqOOUTAjEXNbBeAr',  
  5. 'uid':self.username,  
  6. 'userType':'browser',  
  7. 'var':'<!--?xml version="1.0"?--><object><int name="fid">3</int><string name="order">date</string><boolean name="desc">true</boolean><boolean name="topFirst">false</boolean><int name="start">0</int><int name="limit">20</int></object>' 

可以看出,兩段代碼的不同之處就是fid的取值不同,其中收件箱為1,發(fā)件箱為3,草稿箱為2。

#p#

3、xml解析

利用ElementTree 類來進(jìn)行xml到字典的轉(zhuǎn)換。在獲取通訊錄的實(shí)例中,主要使用了這一方法。本例子(具體代碼見后文)在收取郵件列表時(shí),并沒有用這一方法,仍然使用的是字符串的處理方法。但這里還是列一下ElementTree 類對(duì)xml的處理。如(參考地址:http://hi.baidu.com/fc_lamp/blog/item/8ed2d53ada4586f714cecb3d.html):

  1. -<result>  
  2.    <code>S_OK</code>  
  3.  -<array name="var">  
  4.   -<object>  
  5.      <string name="code">S_OK</string>  
  6.     -<array name="var">  
  7.      +<object></object>  
  8.      +<object></object>  
  9.      +<object></object>  
  10.      +<object></object>  
  11.      +<object></object>  
  12.      +<object></object>  
  13.      +<object></object>  
  14.      +<object></object>  
  15.      +<object></object>  
  16.      +<object></object>  
  17.      +<object></object>  
  18.      +<object></object>  
  19.      +<object></object>  
  20.      +<object></object>  
  21.      +<object></object>  
  22.      +<object></object>  
  23.      </array>  
  24.    </object>  
  25.   +<object></object>  
  26.   </array>  
  27.  </result> 

解決方法:

  1. #-*- coding:UTF-8 -*-  
  2.  
  3. import xml.etree.ElementTree as etree #xml解析類  
  4. def xml2json(xml):  
  5.     json = []  
  6.     tree = etree.fromstring(xml) #如果是文件可用parse(source)  
  7.     obj = None 
  8.     for child in tree:  
  9.         if child.tag == 'array':  
  10.             obj = child              
  11.             break 
  12.     #這里多參考一下,etree元素的方法屬性等,包括attrib,text,tag,getchildren()等  
  13.     obj = obj[0].getchildren().pop()  
  14.     for child in obj:  
  15.         for x in child:  
  16.             attr = x.attrib  
  17.             if attr['name']== 'EMAIL;PREF':  
  18.                 value = {'email':x.text}  
  19.                 json.append(value)  
  20.     return json 

#p#

4、收件箱郵件列表

本例子只列出了收件箱郵件列表,如果需要,可根據(jù)以上介紹調(diào)整fid值,列出發(fā)件箱、草稿箱等的郵件列表。程序在windosxp、py2.6環(huán)境下調(diào)查通過,運(yùn)行后,會(huì)在當(dāng)前目錄下生成三個(gè)文件:inboxlistfile.txt記錄收件箱郵件列表,addfile.txt記錄通訊錄,cookies.dat記錄cookies。具體代碼如下:

  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對(duì)象  
  11.     cookiefile = './cookies.dat' #cookie臨時(shí)存放地  
  12.     user = '' 
  13.       
  14.     def __init__(self,username,passwd):  
  15.         self.username = username  
  16.         self.passwd = passwd  
  17.         #cookie設(shè)置  
  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.         #請(qǐng)求參數(shù)設(shè)置  
  26.         postdata = {  
  27.             'username':self.username,  
  28.             'password':self.passwd,  
  29.             'type':1 
  30.             }  
  31.         postdata = urllib.urlencode(postdata)  
  32.  
  33.         #發(fā)起請(qǐng)求  
  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,#請(qǐng)求數(shù)據(jù)  
  37.                 headers = self.header #請(qǐng)求頭  
  38.             )  
  39.  
  40.         result = urllib2.urlopen(req).read()  
  41.         result = str(result)  
  42.         #print result  
  43.         self.user = self.username.split('@')[0]  
  44.  
  45.         self.cookie.save(self.cookiefile)#保存cookie  
  46.           
  47.         if '登錄成功,正在跳轉(zhuǎn)...' in result:  
  48.             #print("%s 你已成功登陸163郵箱。---------n" %(user))  
  49.             flag = True 
  50.         else:  
  51.             flag = '%s 登陸163郵箱失敗。'%(self.user)  
  52.              
  53.         return flag  
  54.  
  55.    #獲取通訊錄  
  56.     def address_list(self):  
  57.  
  58.         #獲取認(rèn)證sid  
  59.         auth = urllib2.Request(  
  60.                 url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?username='+self.user+'&lightweight=1&verifycookie=1&language=-1&style=1',  
  61.                 headers = self.header  
  62.             )  
  63.         auth = urllib2.urlopen(auth).read()  
  64.  
  65.         #authstr=str(auth)  
  66.         #print authstr  
  67.           
  68.         for i,sid in enumerate(self.cookie):  
  69.             sid = str(sid)  
  70.             #print 'sid:%s' %sid  
  71.             if 'sid' in sid:  
  72.                 sid = sid.split()[1].split('=')[1]  
  73.                 break 
  74.         self.cookie.save(self.cookiefile)  
  75.           
  76.         #請(qǐng)求地址  
  77.         url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username  
  78.         #參數(shù)設(shè)定(var 變量是必需要的,不然就只能看到:<code>S_OK</code><messages>這類信息)  
  79.         #這里參數(shù)也是在firebug下查看的。  
  80.         postdata = {  
  81.             'func':'global:sequential',  
  82.             'showAd':'false',  
  83.             'sid':'qACVwiwOfuumHPdcYqOOUTAjEXNbBeAr',  
  84.             'uid':self.username,  
  85.             'userType':'browser',  
  86.             '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>' 
  87.             }  
  88.         postdata = urllib.urlencode(postdata)  
  89.           
  90.         #組裝請(qǐng)求  
  91.         req = urllib2.Request(  
  92.             url = url,  
  93.             data = postdata,  
  94.             headers = self.header  
  95.             )  
  96.         res = urllib2.urlopen(req).read()  
  97.  
  98.         #print str(res)  
  99.           
  100.         #解析XML,轉(zhuǎn)換成json  
  101.         #說明:由于這樣請(qǐng)求后163給出的是xml格式的數(shù)據(jù),  
  102.         #為了返回的數(shù)據(jù)能方便使用最好是轉(zhuǎn)為JSON  
  103.         json = []  
  104.         tree = etree.fromstring(res)  
  105.  
  106.           
  107.           
  108.         obj = None 
  109.         for child in tree:  
  110.             if child.tag == 'array':  
  111.                 obj = child              
  112.                 break 
  113.         #這里多參考一下,etree元素的方法屬性等,包括attrib,text,tag,getchildren()等  
  114.         obj = obj[0].getchildren().pop()  
  115.         for child in obj:  
  116.             for x in child:  
  117.                 attr = x.attrib  
  118.                 if attr['name']== 'EMAIL;PREF':  
  119.                     value = {'email':x.text}  
  120.                     json.append(value)  
  121.         return json  
  122. #獲取收件箱  
  123.     def minbox(self):  
  124.         #獲取認(rèn)證sid  
  125.         auth = urllib2.Request(  
  126.                 url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?username='+self.user+'&lightweight=1&verifycookie=1&language=-1&style=1',  
  127.                 headers = self.header  
  128.             )  
  129.         auth = urllib2.urlopen(auth).read()  
  130.  
  131.         #authstr=str(auth)  
  132.         #print authstr  
  133.           
  134.         for i,sid in enumerate(self.cookie):  
  135.             sid = str(sid)  
  136.             #print 'sid:%s' %sid  
  137.             if 'sid' in sid:  
  138.                 sid = sid.split()[1].split('=')[1]  
  139.                 break 
  140.         self.cookie.save(self.cookiefile)  
  141.           
  142.           
  143.         url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=mbox:listMessages&showAd=false&userType=browser&uid='+self.username  
  144.           
  145.         postdata = {  
  146.             'func':'global:sequential',  
  147.             'showAd':'false',  
  148.             'sid':'qACVwiwOfuumHPdcYqOOUTAjEXNbBeAr',  
  149.             'uid':self.username,  
  150.             'userType':'browser',  
  151.             'var':'<!--?xml version="1.0"?--><object><int name="fid">1</int><string name="order">date</string><boolean name="desc">true</boolean><boolean name="topFirst">false</boolean><int name="start">0</int><int name="limit">20</int></object>' 
  152.             }  
  153.         postdata = urllib.urlencode(postdata)  
  154.           
  155.         #組裝請(qǐng)求  
  156.         req = urllib2.Request(  
  157.             url = url,  
  158.             data = postdata,  
  159.             headers = self.header  
  160.             )  
  161.         res = urllib2.urlopen(req).read()  
  162.  
  163.         liststr=str(res).split('<object>')#用object進(jìn)行分割  
  164.         inboxlistcount=len(liststr)-1#記錄郵件封數(shù)  
  165.         inboxlistfile=open('inboxlistfile.txt','w')  
  166.         t=0  #記錄當(dāng)前第幾封信  
  167.         for i in liststr:  
  168.             if 'xml' in i and ' version=' in i:  
  169.                 inboxlistfile.write('inbox 共'+str(inboxlistcount)+'信')  
  170.                 inboxlistfile.write('\n')  
  171.             if 'name="id"' in i:  
  172.                 t=t+1 
  173.                 inboxlistfile.write('第'+str(t)+'封:')  
  174.                 inboxlistfile.write('\n')  
  175.                 #寫入from  
  176.                 beginnum=i.find('name="from"')  
  177.                 endnum=i.find('</string>',beginnum)  
  178.                 inboxlistfile.write('From:'+i[beginnum+12:endnum])  
  179.                 inboxlistfile.write('\n')  
  180.                 #寫入to  
  181.                 beginnum=i.find('name="to"')  
  182.                 endnum=i.find('</string>',beginnum)  
  183.                 inboxlistfile.write('TO:'+i[beginnum+10:endnum])  
  184.                 inboxlistfile.write('\n')  
  185.                 #寫入subject  
  186.                 beginnum=i.find('name="subject"')  
  187.                 endnum=i.find('</string>',beginnum)  
  188.                 inboxlistfile.write('Subject:'+i[beginnum+15:endnum])  
  189.                 inboxlistfile.write('\n')  
  190.                 #寫入date:  
  191.                 beginnum=i.find('name="sentDate"')  
  192.                 endnum=i.find('</date>',beginnum)  
  193.                 inboxlistfile.write('Date:'+i[beginnum+16:endnum])  
  194.                 inboxlistfile.write('\n')  
  195.                 if 'name="read">true' in i:  
  196.                     inboxlistfile.write('郵件狀態(tài):已讀')  
  197.                     inboxlistfile.write('\n')  
  198.                 else:  
  199.                     inboxlistfile.write('郵件狀態(tài):未讀')  
  200.                     inboxlistfile.write('\n')  
  201.                 #寫用郵件尺寸  
  202.                 beginnum=i.find('name="size"')  
  203.                 endnum=i.find('</int>',beginnum)  
  204.                 inboxlistfile.write('郵件尺寸:'+i[beginnum+12:endnum])  
  205.                 inboxlistfile.write('\n')  
  206.                 #寫入郵件編號(hào),用于下載郵件  
  207.                 beginnum=i.find('name="id"')  
  208.                 endnum=i.find('</string>',beginnum)  
  209.                 inboxlistfile.write('郵件編號(hào):'+i[beginnum+10:endnum])  
  210.                 inboxlistfile.write('\n\n')  
  211.                   
  212.         inboxlistfile.close()  
  213.                   
  214.           
  215.           
  216. if __name__=='__main__':  
  217.     print("Edit @xiaowuyi V1.0  http://www.cnblogs.com/xiaowuyi")  
  218.     login = Login163('XXXX@163.com','AAAAA')  
  219.     flag = login.login()  
  220.     if type(flag) is bool:  
  221.       
  222.     #login.letterdown()  
  223.         print("登陸成功,正在下載列表和通訊錄………………")  
  224.         login.minbox()  
  225.         res = login.address_list()  
  226.         addfile=open('addfile.txt','w')  
  227.         for x in res:  
  228.             addfile.write(x['email'])  
  229.         addfile.close()  
  230.         print("已完成")  
  231.     else:  
  232.         print(flag) 

原文鏈接:http://www.cnblogs.com/xiaowuyi/archive/2012/05/21/2511428.html

責(zé)任編輯:張偉 來源: 小五義的博客
相關(guān)推薦

2018-07-13 08:56:16

編程語言Python

2021-06-02 14:45:52

遠(yuǎn)程服務(wù)器Python

2011-07-20 09:27:37

Scala

2010-04-21 17:20:03

Unix遠(yuǎn)程

2011-03-08 13:52:25

Proftpd

2021-10-09 08:07:56

Python 3.11Microsoft S應(yīng)用商店

2009-07-16 15:14:27

WebWork用戶登陸

2012-02-14 10:46:15

WP Marketpl雜志月刊

2009-12-15 17:28:58

戴爾互聯(lián)課堂

2010-08-18 08:21:49

Adobe AIRAndroid

2009-02-18 22:19:24

AD用戶登陸實(shí)現(xiàn)限制

2012-11-07 10:09:11

組件技術(shù)OAuth授權(quán)登陸

2011-02-25 17:07:25

2020-03-01 17:04:17

Python數(shù)據(jù)運(yùn)維

2009-01-12 17:34:11

服務(wù)器虛擬化VMware

2009-08-16 20:24:59

linux命令行登陸linux命令行linux命令

2017-11-14 08:25:36

數(shù)據(jù)庫(kù)MySQL安全登陸

2010-11-22 17:41:39

Ubuntu OneWindows

2012-01-10 11:52:39

宏碁AcerCloud宏基Web服務(wù)器

2009-08-14 09:40:55

MyEclipse整合
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)