日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python下载邮箱附件_基于Python3 下载邮箱附件,并解压到指定文件夹

發布時間:2023/12/19 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python下载邮箱附件_基于Python3 下载邮箱附件,并解压到指定文件夹 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#!/usr/bin/env?python3

#?-*-?coding:?utf-8?-*-

#?-*-?encoding:?gbk?-*-

#?目前只測試過網易163郵箱,qq郵箱時間格式與163有所不同需要更改時間格式那部分的代碼

import?poplib

import?email

import?datetime

import?time

from?email.parser?import?Parser

from?email.header?import?decode_header

from?email.utils?import?parseaddr

import?traceback

import?sys

import?telnetlib

import?zipfile

import?os

import?shutil

#?from?email.utils?import?parseaddr

class?c_step4_get_email:

"""創建一個處理郵箱附件下載和解壓的類"""

#?@staticmethod

def?decode_str(str_in):

"""字符編碼轉換"""

value,?charset?=?decode_header(str_in)[0]

if?charset:

value?=?value.decode(charset)

return?value

#?@staticmethod

def?get_att(msg_in,?str_day_in):

"""解析郵件,獲取附件"""

#?import?email

attachment_files?=?[]

for?part?in?msg_in.walk():

#?獲取附件名稱類型

file_name?=?part.get_filename()

#?contType?=?part.get_content_type()

if?file_name:

h?=?email.header.Header(file_name)

#?對附件名稱進行解碼

dh?=?email.header.decode_header(h)

filename?=?dh[0][0]

if?dh[0][1]:

#?將附件名稱可讀化

filename?=?c_step4_get_email.decode_str(str(filename,?dh[0][1]))

print(filename)

#?filename?=?filename.encode("utf-8")

#?下載附件

data?=?part.get_payload(decode=True)

#?在指定目錄下創建文件,注意二進制文件需要用wb模式打開

att_file?=?open('C:\\Users\\Administrator\\Desktop\\銀聯對賬zip文件臨時存放文件夾'?+?'\\'?+?filename,?'wb')

attachment_files.append(filename)

att_file.write(data)??#?保存附件

att_file.close()

return?attachment_files

#?@staticmethod

def?run_ing():

"""登陸郵箱"""

#?輸入郵件地址,?口令和POP3服務器地址:

email_user?=?'******@163.com'

#?此處密碼是授權碼,用于登錄第三方郵件客戶端

password?=?'******'

pop3_server?=?'pop.163.com'

#?日期賦值

day?=?datetime.date.today()

str_day?=?str(day).replace('-',?'')

print(str_day)

#?連接到POP3服務器,有些郵箱服務器需要ssl加密,可以使用poplib.POP3_SSL

try:

telnetlib.Telnet('pop.163.com',?995)

server?=?poplib.POP3_SSL(pop3_server,?995,?timeout=10)

except:

time.sleep(5)

server?=?poplib.POP3(pop3_server,?110,?timeout=10)

#?可以打開或關閉調試信息

server.set_debuglevel(1)

#?打印POP3服務器的歡迎文字:

print(server.getwelcome().decode('utf-8'))

#?身份認證:

server.user(email_user)

server.pass_(password)

#?返回郵件數量和占用空間:

print('Messages:?%s.?Size:?%s'?%?server.stat())

#?list()返回所有郵件的編號:

resp,?mails,?octets?=?server.list()

#?可以查看返回的列表類似[b'1?82923',?b'2?2184',?...]

print(mails)

index?=?len(mails)

#?倒序遍歷郵件

#?for?i?in?range(index,?0,?-1):

#?順序遍歷郵件

a?=?1

while?a?==?1:

global?number

for?i?in?range(number,?index+1):

resp,?lines,?octets?=?server.retr(i)

#?lines存儲了郵件的原始文本的每一行,

#?郵件的原始文本:

msg_content?=?b'\r\n'.join(lines).decode('unicode_escape')

#?解析郵件:

msg?=?Parser().parsestr(msg_content)

From?=?parseaddr(msg.get('from'))[1]???????????#?發件人

To?=?parseaddr(msg.get('To'))[1]???????????????#?收件人

Cc?=?parseaddr(msg.get_all('Cc'))?[1]?????????????#?抄送人

Subject?=?parseaddr(msg.get('Subject'))[1]????????#主題

print('from:%s,to:%s,Cc:%s,Subject%s:'%(From,To,Cc,Subject))

if?From?==?'operator_gd@unionpay.com':?????????#?判斷是否來自指定郵箱

#?獲取郵件時間,格式化收件時間

date1?=?time.strptime(msg.get("Date")[0:24],?'%a,?%d?%b?%Y?%H:%M:%S')

#?郵件時間格式轉換

date2?=?time.strftime("%Y%m%d",?date1)

if?date2?

#?倒敘用break

#?break

#?順敘用continue

continue

else:

#?獲取附件

c_step4_get_email.get_att(msg,?str_day)

a?+=?1

else:

number?+=?1

#?c_step4_get_email.run_ing()

server.quit()

def?un_zip(file_name):

"""解壓單個文件"""

zip_file?=?zipfile.ZipFile(file_name)???????????#讀取zip文件

for?names?in?zip_file.namelist():

zip_file.extract(names,local)???????????????#解壓

zip_file.close()

if?os.path.exists(file_name):?????????????????#用os中rename?重命名的方法剪貼zip文件

os.rename(file_name,zip_path?+?file_name[48:])

print(file_name,'解壓成功')

def?un_zip_Tree(path):

"""解壓文件夾中的zip文件"""

if?not?os.path.exists(path):????????????????????#?如果本地文件夾不存在,則創建它

os.makedirs(path)

for?file?in?os.listdir(path):???????????????????#listdir()返回當前目錄下清單列表

Local?=?os.path.join(path,?file)?????????????#os.path.join()用于拼接文件路徑

if?os.path.isdir(file):??????????????????????#?判斷是否是文件

if?not?os.path.exists(Local):???????????#對于文件夾:如果本地不存在,就創建該文件夾

os.makedirs(Local)

un_zip_Tree(path)

else:??#?是文件

if?os.path.splitext(Local)[1]?==?'.zip':?#os.path.splitext(Remote)[1]獲取文件擴展名,判斷是否為.zip文件

c_step4_get_email.un_zip(Local)??????#解壓文件

if?__name__?==?'__main__':

origin?=?sys.stdout

number?=?1

zip_path?=?'C:\\Users\\Administrator\\Desktop\\'

local?=?'C:\\Users\\Administrator\\Desktop\\'?????????#?此處看實際解壓到的存儲路徑

path?=?'C:\\Users\\Administrator\\Desktop\\'??????????#?此處看實際存儲壓縮文件的路徑

#?簡單的日志處理??將日志寫入到log.txt

f?=?open('E:\python_project\log.txt',?'w')

sys.stdout?=?f

try:

c_step4_get_email.run_ing()

except?Exception?as?e:

s?=?traceback.format_exc()

print(e)

tra?=?traceback.print_exc()

sys.stdout?=?origin

c_step4_get_email.un_zip_Tree(path)

f.close()

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python下载邮箱附件_基于Python3 下载邮箱附件,并解压到指定文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。