我想抓取嵌入式電子郵件中的圖像。問題是,我保存的圖像是不可讀的,我無法弄清楚爲什麼。 電子郵件(保存爲我的代碼開始加載一個文件):用python保存電子郵件中的嵌入式圖像
MIME-Version: 1.0
Received: by 10.100.120.7 with HTTP; Tue, 18 Oct 2011 10:36:48 -0700 (PDT)
In-Reply-To: <[email protected]e.com>
References: <[email protected]e.com>
Date: Tue, 18 Oct 2011 19:36:48 +0200
Delivered-To: [email protected]
Message-ID: <[email protected]om>
Subject: openme
From: Simeon Shpiz <[email protected]>
To: me <[email protected]>
Content-Type: multipart/related; boundary=001636c5977303b92404af962ba6
--001636c5977303b92404af962ba6
Content-Type: multipart/alternative; boundary=001636c5977303b91d04af962ba5
--001636c5977303b91d04af962ba5
Content-Type: text/plain; charset=ISO-8859-1
****
--001636c5977303b91d04af962ba5
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_quote"><div lang=3D"EN-US" link=3D"blu=
e" vlink=3D"purple"><div><p class=3D"MsoNormal"><span style=3D"font-size:11=
.0pt;color:#1F497D"><img width=3D"15" height=3D"13" src=3D"cid:image003.png=
@01CC8DCD.30A2A7C0"></span><span style=3D"font-size:11.0pt;color:#1F497D"><=
u></u><u></u></span></p>
</div>
</div></div><br></div>
--001636c5977303b91d04af962ba5--
--001636c5977303b92404af962ba6
Content-Type: image/png; name="image003.png"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
X-Attachment-Id: 3e79c375acccec3d_0.1
iVBORw0KGgoAAAANSUhEUgAAAA4AAAANCAIAAAAWvsgoAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAO
yAAADsMBrahYpwAAAItJREFUKFNj/P//PwNxgIk4ZWBVQFOBoBsMsGqrqqr6CgYsaNIPHz6EiMjJ
yb19+xbISE9PLy4uBjLQlSLrFBYWBnITExN9fHyADMJulZCQgOgnrFRUVJRYpXAnETb19evXxJr6
4sULiFJ8IfDt2zegii1btmRkZGBRKi8vjxbSwKjJysoCCjISnwYATtwwhahioZoAAAAASUVORK5C
YII=
--001636c5977303b92404af962ba6--
的Python代碼我使用:
import email
from BeautifulSoup import BeautifulSoup
message = email.message_from_file(open(r'C:\shpiz\test\msg\12248'))
cid_list = []
images = []
for part in message.walk():
if str(part.get_content_type()) == 'text/html':
soup = BeautifulSoup(part.get_payload(decode=True))
cid = '<%s>'%soup('img')[0]['src'][4:]
cid_list.append(cid)
for part in message.walk():
if part.get('Content-ID') in cid_list :
images.append((part.get_filename(),part.get_payload(decode=True)))
for name, image in images:
with open(r'c:\shpiz\test\%s'%name,'w') as f:
f.write(image)
保存的圖像是不幸的是沒有好。 (沒有程序打開它)。
我用notepad ++查看了原始和新的圖像文件,並且有一個區別 - 看起來有一個換行符,我的生成的副本不存在於原始文件中。這不是唯一的區別,但刪除記事本+ +中的行並沒有使圖像可打開。我所描述的差異可以看出here
希望在尋找問題的任何幫助。
謝謝。這是問題所在。 –