Python:msg.get_payload()丢弃所需的数据,需要解决方案

时间:2012-07-15 00:54:06

标签: python regex parsing email pop3

您好我在这里经历了各种各样的帖子,但没有人回答我的问题,我有两个问题, 1。 我写了一个脚本来使用poplib获取电子邮件,everythig工作正常,直到我试图解析电子邮件的正文,它摆脱了标签加上里面的数据,我现在放弃了,我在这里寻求帮助希望你那些人会引导我走向正确的方向,因为我在做错了,或者我应该做些什么来使它发挥作用。

这是解析器脚本的来源

import sys
import socket
import poplib
import email
import csv
import re
try:
  host = "mail.someserver.com"
  mail = poplib.POP3(host)
  print mail.getwelcome()
  print mail.user("username@someserver.com")
  print mail.pass_("qaiaJWkvZT")
  print mail.stat()
  print mail.list()
  print ""

  emailWriter = csv.writer(open('emailMessages.csv', 'wb'), delimiter=',', quotechar='\'', quoting=csv.QUOTE_MINIMAL)
  emailWriter.writerow(['Messages'])
  if mail.stat()[1] > 0:
      print "You have new mail."
  else:
      print "No new mail."

  print ""

  numMessages = len(mail.list()[1])
  for i in range(numMessages):
      for j in mail.retr(i+1)[1]:
          #print j
          msg = email.message_from_string(j) # new statement
          print msg.get_payload(decode=True)
          #emailWriter.writerow([msg.get_payload(decode=True)]) # new statement

  mail.quit()
  input("Press any key to continue.")
except socket.error as e:
  print "Something went wrong! :(\nREASON:\n{0}:{1}".format(e.errno, e.strerror)
  raise
except:
  print "Something went wrong!", sys.exc_info()[0]
  raise

以下是上述脚本生成的输出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or
g/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
BODY {







}
TD {



}
TH {


}
H1 {

}
TABLE,IMG,A {

}
</style>
</head>
<body>


<p><strong>PO Number:</strong> 35164</p>

<p><strong>Ship To:</strong><br />
Joe Pasloski<br />
16 Redwood Drive<br />Yorkton, SK  S3N2X7<br />
204-473-2218</p>


<table cellspacing="0" cellpadding="5" border="1" width="710" align="left">
<tr>



</tr>
<tr>



</tr>
</table>
</body>
</html>

但是,如果我将脚本更改为直接在循环内打印j变量,它会给我这个

Return-Path: <orders@someserver.com>
Delivered-To: username@someserver.com
Received: (qmail 7636 invoked by uid 48); 14 Jul 2012 23:29:11 -0000
Date: 14 Jul 2012 23:29:11 -0000
Message-ID: <20120714232911.7635.qmail@b.inetuhosted.net>
To: username@someserver.com
Subject: Drop Ship Order - Joe Pasloski
From: Someserver.com <orders@someserver.com>
X-Mailer: PHP/5.2.17
MIME-Version: 1.0
Content-Type: multipart/alternative;
         boundary="2631183869_50020"
Reply-to: SomeServer.com <orders@someserver.com>
X-Antivirus: avast! (VPS 120714-2, 07/15/2012), Inbound message
X-Antivirus-Status: Clean

--2631183869_50020
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: 8bit



--2631183869_50020
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or
g/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
BODY {
        MARGIN-TOP: 10px;
        MARGIN-BOTTOM: 10px;
        MARGIN-LEFT: 10px;
        MARGIN-RIGHT: 10px;
        FONT-SIZE: 12px;
        FONT-FAMILY: arial,helvetica,sans-serif
        PADDING: 0px;
}
TD {
        FONT-SIZE: 12px;
        FONT-FAMILY: arial,helvetica,sans-serif
        COLOR: #000000;
}
TH {
        FONT-SIZE: 13px;
        FONT-FAMILY: arial,helvetica,sans-serif
}
H1 {
    FONT-SIZE: 20px
}
TABLE,IMG,A {
        BORDER: 0px;
}
</style>
</head>
<body>


<p><strong>PO Number:</strong> 35164</p>

<p><strong>Ship To:</strong><br />
Joe Pasloski<br />
16 Redwood Drive<br />Yorkton, SK  S3N2X7<br />
204-473-2218</p>

<p><strong>Items:</strong>
<table cellspacing="0" cellpadding="5" border="1" width="710" align="left">
<tr>
        <th align="left" width="100">SKU</th>
        <th align="left" width="550">Product</th>
        <th align="left" width="60">Qty</th>
</tr>
<tr>
        <td>JJ-Hamper-Firetruck</td>
        <td>Frankie's Fire Truck Laundry Hamper</td>
        <td>1</td>
</tr>
</table>
</body>
</html>

如果我需要处理原始消息,我怎样才能有效地获取消息的正文部分,删除不必要的html标记而不丢失任何数据?或者,如果可以通过使用get_payload()方法,我该怎么做才能使它工作。

请帮忙!

2。 还有一种方法可以使用正则表达式获取表中包含的所有SKU信息吗?如果你能提供给我那将是一个加号。万分感谢

1 个答案:

答案 0 :(得分:1)

好的,我自己找到了答案,文档说明全部,Python: How to get HTML body of an email message using poplib?的帖子帮助我朝着正确的方向前进。我知道我正在处理的消息不是多部分类型,而是应用了get_payload()丢失了html数据,这就是为什么我必须实现一些正则表达式例程来从原始消息中去除html标签,因为我在原始消息上下载并使用了Aaron Swartz的html2text库,然后执行了msg.get_payload() ..这就是我所做的

import html2text # added to my source
numMessages = len(mail.list()[1])
  for i in range(numMessages):
      for j in mail.retr(i+1)[1]:

          msg = email.message_from_string(html2text.html2text(j)) 
          print msg.get_payload(decode=False)

反过来给了我

charset="iso-8859-1"











BODY {









}


TD {





}


TH {




}


H1 {



}


TABLE,IMG,A {



}










**PO Number:** 35170




**Ship To:**


Tami Curtis


67 E. Spring Creek Pkwy

Providence, UT 84332


4357553197









SKU


Product


Qty






JJ-Panel-Isabella-BK-PRT


Isabella Black Damask Curtains (2 Panels)


1

现在我只需要用正则表达式来清理它,以获得不必要的换行/空格和css标记的reif。

希望它对其他人也有帮助:)干杯!