如何从电子邮件正文Python中解析HTML

2024-06-16 08:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python解析传入的电子邮件。 我收到的电子邮件是部分文本部分HTML。 我想得到HTML部分并在HTML中找到一个表。

我试过用beatifulsoup。但是当尝试下一个代码时,bs只得到第一个“”部分,而不是所有的HTML部分:

# connecting to the gmail imap server
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)
# use m.list() to get all the mailboxes, "INBOX" to get only inbox
m.select("INBOX")
resp, items = m.search(None, '(UNSEEN)') # you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
items = items[0].split() # getting the mails id

for emailid in items:
    # getting the mail content
    resp, data = m.fetch(emailid, '(UID BODY[TEXT])')
    text = str(data[0][1])
    soup = bs(text)

如何对整个HTML部分使用'bs'? 或者,有没有其他方法可以从电子邮件正文中解析出一个html表?

“bs”对我来说似乎是最好的,因为我想找到包含特定关键字的特定HTML正文,“bs”搜索可以检索整个表并让我在其中迭代。


Tags: thetocomsearchgetbs电子邮件html