gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:12:44 24 4
gpt4 key购买 nike

我正在尝试用 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 Body,'bs' 搜索可以检索整个表格并让我在其中迭代。

最佳答案

显然,我使用了错误的解析器。

一旦我更改为“lxml”解析器,它就可以正常工作了。

需要改变下一行:

soup = bs(text,"lxml");

关于python - 如何从电子邮件正文中解析 HTML - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com