首页 > 解决方案 > Python BeautifulSoup 'Slice' 对象没有属性 'lower'

问题描述

我有这个脚本可以运行以从收件箱的电子邮件中提取数据,但是它在几天前可以工作,但是现在当我运行它时,每当有电子邮件进入“切片对象没有属性较低”时,它都会给我一个错误并说错误发生soup = BeautifulSoup(msg, "html.parser")在线路上。这是什么意思,我该如何解决?这是代码:

import email
import imaplib
from bs4 import BeautifulSoup
import time
import sys

username = 'xx.xxx@xxx.ca'
password = 'xx'

mail = imaplib.IMAP4_SSL('imap-mail.outlook.com')
(retcode, capabilities) = mail.login(username, password)
mail.select('inbox')

n=0
while True:
    (retcode, messages) = mail.search(None, 'UNSEEN', '(SUBJECT "xxxx-    ")', '(FROM "xx.xx@xxxx.ca")')
    if retcode == 'OK':
        for num in messages[0].split():
            n=n+1
            print('Processing Email ' + str(n))
            typ, data = mail.fetch(num, '(RFC822)')
            for response_part in data:
                if isinstance(response_part, tuple):
                    original = email.message_from_bytes(response_part[1])
                    print("Subject: " + original['Subject'])
                    body = data[0][1]
                    msg = email.message_from_bytes(body)
                    content = msg.get_payload(decode=True)
                    soup = BeautifulSoup(msg, "html.parser")
                    text = soup.get_text()
                    fontdef, email1 = text.split('-->')
                    email2, signature =     email1.split('________________________________________________')
                    print(email2)
                    typ, data = mail.store(num,'+FLAGS','\\Seen')
                    time.sleep(10)

标签: pythonemailbeautifulsoupimaplib

解决方案


推荐阅读