切换风格

Wizard Sky California Sunset glow Black Cloud Beige Dragon Lavender NewYear City Snow Flowers London
收藏本站XSS平台字符串转换jsfuck
python模拟登陆163邮箱并下载邮件内容[复制链接]
发表于 2013-8-8 23:08:16 | 显示全部楼层 |!read_mode!
1 由于163邮箱有众多版本,所以登录请求URL,邮件URL等可能也不太一样,下面是163的简约3.0
2 代码缺乏错误处理能力,有待优化。
3 更多参考:http://fc-lamp.blog.163.com/blog/static/17456668720137844546560/
#coding:utf-8
'''
    模拟登陆163邮箱并下载邮件内容
    @author: fc_lamp
    @blog:fc-lamp.blog.163.com
'''
import urllib
import urllib2
import cookielib
import re
import time
import json

class Email163:
    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'}
    user = ''
    cookie = None
    sid = None
    mailBaseUrl='http://twebmail.mail.163.com'

    def __init__(self):
        self.cookie = cookielib.CookieJar()
        cookiePro = urllib2.HTTPCookieProcessor(self.cookie)
        urllib2.install_opener(urllib2.build_opener(cookiePro))

    def login(self,user,pwd):
        '''
            登录
        '''
        postdata = urllib.urlencode({
                'username':user,
                'password':pwd,
                'type':1
            })
        #注意版本不同,登录URL也不同
        req = urllib2.Request(
                url='https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?funcid=loginone&language=-1&passtype=1&iframe=1&product=mail163&from=web&df=email163&race=-2_45_-2_hz&module=&uid='+user+'&style=10&net=t&skinid=null',
                data=postdata,
                headers=self.header,
            )
        res = str(urllib2.urlopen(req).read())
        #print res
        patt = re.compile('sid=([^"]+)',re.I)
        patt = patt.search(res)
        
        uname = user.split('@')[0]
        self.user = user
        if patt:
            self.sid = patt.group(1).strip()
            #print self.sid
            print '%s Login Successful.....'%(uname)
        else:
            print '%s Login failed....'%(uname)            


    def getInBox(self):
        '''
            获取邮箱列表
        '''
        print '\nGet mail lists.....\n'
        sid = self.sid
        url = self.mailBaseUrl+'/jy3/list/list.do?sid='+sid+'&fid=1&fr=folder'
        res = urllib2.urlopen(url).read()
        #获取邮件列表
        mailList = []
        patt = re.compile('<div\s+class="tdLike Ibx_Td_From"[^>]+>.*?href="([^"]+)"[^>]+>(.*?)<\/a>.*?<div\s+class="tdLike Ibx_Td_Subject"[^>]+>.*?href="[^>]+>(.*?)<\/a>',re.I|re.S)
        patt = patt.findall(res)
        if patt==None:
            return mailList
        
        for i in patt:
            line =  {
                    'from':i[1].decode('utf8'),
                     'url':self.mailBaseUrl+i[0],
                     'subject':i[2].decode('utf8')
                     }
            mailList.append(line)

        return mailList
        

    def getMailMsg(self,url):
        '''
            下载邮件内容
        '''
        content=''
        print '\n Download.....%s\n'%(url)
        res = urllib2.urlopen(url).read()
        
        patt = re.compile('contentURL:"([^"]+)"',re.I)
        patt = patt.search(res)
        if patt==None:
            return content
        url = '%s%s'%(self.mailBaseUrl,patt.group(1))
        time.sleep(1)
        res = urllib2.urlopen(url).read()
        Djson = json.JSONDecoder(encoding='utf8')
        jsonRes = Djson.decode(res)
        if 'resultVar' in jsonRes:
            content = Djson.decode(res)['resultVar']
        time.sleep(3)
        return content
        
            
'''
    Demon
'''
#初始化
mail163 = Email163()
#登录
mail163.login('xxxxx@163.com','xxxx')
time.sleep(2)

#获取收件箱
elist = mail163.getInBox()

#获取邮件内容
for i in elist:
    print '主题:%s   来自:%s  内容:\n%s'%(i['subject'].encode('utf8'),i['from'].encode('utf8'),mail163.getMailMsg(i['url']).encode('utf8'))


2. [图片] 结果.png
08170102_r0JB.png






该贴已经同步到 抚菊深思的微博
操千曲而后晓声,观千剑而后识器。
发表于 2013-8-9 09:46:54 | 显示全部楼层
Very cool, I will verify it this evening. Thanks.
发表于 2013-8-9 09:50:12 | 显示全部楼层
rock 发表于 2013-8-9 09:46
Very cool, I will verify it this evening. Thanks.


发表于 2013-8-9 14:45:00 | 显示全部楼层
老大 v5 !!
发表于 2014-3-9 17:40:08 | 显示全部楼层
这个程序很好啊,今天看了下,转为python3.3的了

#coding:utf-8
'''
    模拟登陆163邮箱并下载邮件内容
    @author: fc_lamp
    @blog:fc-lamp.blog.163.com
'''
import urllib.request, urllib.parse, urllib.error
import http.cookiejar
import re
import time
import json
import os

def dump(filepath,filename,content):
    if content == None:
        return
    with open(os.path.join(filepath,filename+'.html'), 'wb') as f:
            f.write(content.encode('utf8'))

class Email163:
    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'}
    user = ''
    cookie = None
    sid = None
    mailBaseUrl='http://twebmail.mail.163.com'

    def __init__(self):
        self.cookie = http.cookiejar.CookieJar()
        cookiePro = urllib.request.HTTPCookieProcessor(self.cookie)
        urllib.request.install_opener(urllib.request.build_opener(cookiePro))
    def login(self,user,pwd):
        '''
            登录
        '''
        postdata = urllib.parse.urlencode({
                'username':user,
                'password':pwd,
                'type':1
            })
        #注意版本不同,登录URL也不同
        req = urllib.request.Request(
                url='https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?funcid=loginone&language=-1&passtype=1&iframe=1&product=mail163&from=web&df=email163&race=-2_45_-2_hz&module=&uid='+user+'&style=10&net=t&skinid=null',
                data=postdata.encode('utf8'),
                headers=self.header,
            )
        res = str(urllib.request.urlopen(req).read())
        #print res
        patt = re.compile('sid=([^"]+)',re.I)
        patt = patt.search(res)
        
        uname = user.split('@')[0]
        self.user = user
        if patt:
            self.sid = patt.group(1).strip()
            #print self.sid
            print('%s Login Successful.....'%(uname))
        else:
            print('%s Login failed....'%(uname))         


    def getInBox(self):
        '''
            获取邮箱列表
        '''
        print ('\nGet mail lists.....\n')
        sid = self.sid
        url = self.mailBaseUrl+'/jy3/list/list.do?sid='+sid+'&fid=1&fr=folder'
        res = urllib.request.urlopen(url, timeout=5).read()
        #获取邮件列表
        mailList = []
        patt = re.compile(b'<div\s+class="tdLike Ibx_Td_From"[^>]+>.*?href="([^"]+)"[^>]+>(.*?)<\/a>.*?<div\s+class="tdLike Ibx_Td_Subject"[^>]+>.*?href="[^>]+>(.*?)<\/a>',re.I|re.S)
        patt = patt.findall(res)
        if patt==None:
            return mailList
        
        for i in patt:
            line =  {
                    'from':i[1].decode('utf8'),
                     'url':self.mailBaseUrl+i[0].decode('utf8'),
                     'subject':i[2].decode('utf8')
                     }
            mailList.append(line)
        
        print ('\nEmail count: %d\n' % len(mailList))       
        return mailList
        

    def getMailMsg(self,url):
        '''
            下载邮件内容
        '''
        content=''
        print('\n Download.....%s\n'%(url))
        
        res = urllib.request.urlopen(url).read()
        
        patt = re.compile(b'contentURL:"([^"]+)"',re.I)
        patt = patt.search(res)
        if patt==None:
            return content
        url = '%s%s'%(self.mailBaseUrl,patt.group(1).decode('utf8'))
        time.sleep(1)
        res = urllib.request.urlopen(url).read().decode('utf8')
        jsonRes = json.JSONDecoder().decode(res)
        if 'resultVar' in jsonRes:
            content = jsonRes['resultVar']
        time.sleep(3)
        return content
        
            
'''
    Demon
'''
if __name__=='__main__':
    emailAddr = input('input your email address:')
    emailPassword = input('Password:')
    #初始化
    mail163 = Email163()
    #登录
    mail163.login(emailAddr, emailPassword)
    time.sleep(2)
    
    #获取收件箱
    elist = mail163.getInBox()
    
    #获取邮件内容
    for i in elist:
        #print('主题:%s   来自:%s  内容:\n%s'%(i['subject'],i['from'],mail163.getMailMsg(i['url'])))
        dump('./', i['subject'], mail163.getMailMsg(i['url']))

已有 1 人评分威望 金币 贡献 收起 理由
店小二01 + 1 + 5 + 1

总评分: 威望 + 1  金币 + 5  贡献 + 1   查看全部评分

发表于 2014-4-29 22:01:01 | 显示全部楼层
这种是用网页抓取,其实163默认开imap,为什么不直接用imap模块,更方便啊

代码区

GMT+8, 2019-11-13 03:29

Powered by Discuz! X2

© 2001-2018 Comsenz Inc.

回顶部