Python dict KeyError 'RFC822' - Gmail 的 IMAPClient
Python dict KeyError 'RFC822' - IMAPClient for Gmail
所以我是编码初学者,我选择 Python 开始。我正在尝试编写一个脚本来获取从特定联系人收到的最后一封电子邮件的 "date"。这个 "date" 然后将保存在 Google sheet.
以下是我目前拥有的仅处理 Gmail 部分的代码。我实际上使用了 here 中的部分代码。但是,我收到一个错误
Traceback (most recent call last):
File "C:/Users/PycharmProjects/Automate/Code.py", line 33, in
msg_string = data['RFC822']
KeyError: 'RFC822'
不确定出了什么问题。我正在使用 Python 3.8.1
import email
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True
## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
messages = server.search(['FROM', 'email_of_the_contact@gmail.com'])
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.items():
msg_string = data['RFC822']
msg = email.message_from_string(msg_string)
print('ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date']))
此外,不确定代码是否完整,符合我要实现的目标。任何帮助表示赞赏。
此外,添加从调试得到的消息
pydev debugger: process 344 is connecting
Connected to pydev debugger (build 193.6494.30)
Traceback (most recent call last):
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1557, in _conn_request
conn.connect()
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1305, in connect
address_info = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
File "C:\Program Files (x86)\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1982, in request
(response, content) = self._request(
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1650, in _request
(response, content) = self._conn_request(
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1564, in _conn_request
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
httplib2.ServerNotFoundError: Unable to find the server at oauth2.googleapis.com
Process finished with exit code -1
这里我运行通过你的代码重现了同样的错误,我也得到了同样的错误。
为了解决这个问题,我检查了字典键并注意到字典键和值被编码为字节。
所以,我使用字节密钥访问和解码到 t运行slate 消息到 str,如:
import email
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True
## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
messages = server.search(['FROM', 'email_of_the_contact@gmail.com'])
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.items():
msg_string = data[b'RFC822']
msg = email.message_from_string(msg_string.decode())
print('ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date']))
所以我是编码初学者,我选择 Python 开始。我正在尝试编写一个脚本来获取从特定联系人收到的最后一封电子邮件的 "date"。这个 "date" 然后将保存在 Google sheet.
以下是我目前拥有的仅处理 Gmail 部分的代码。我实际上使用了 here 中的部分代码。但是,我收到一个错误
Traceback (most recent call last): File "C:/Users/PycharmProjects/Automate/Code.py", line 33, in msg_string = data['RFC822'] KeyError: 'RFC822'
不确定出了什么问题。我正在使用 Python 3.8.1
import email
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True
## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
messages = server.search(['FROM', 'email_of_the_contact@gmail.com'])
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.items():
msg_string = data['RFC822']
msg = email.message_from_string(msg_string)
print('ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date']))
此外,不确定代码是否完整,符合我要实现的目标。任何帮助表示赞赏。
此外,添加从调试得到的消息
pydev debugger: process 344 is connecting
Connected to pydev debugger (build 193.6494.30)
Traceback (most recent call last):
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1557, in _conn_request
conn.connect()
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1305, in connect
address_info = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
File "C:\Program Files (x86)\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1982, in request
(response, content) = self._request(
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1650, in _request
(response, content) = self._conn_request(
File "C:\Users\PycharmProjects\Automate\venv\lib\site-packages\httplib2\__init__.py", line 1564, in _conn_request
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
httplib2.ServerNotFoundError: Unable to find the server at oauth2.googleapis.com
Process finished with exit code -1
这里我运行通过你的代码重现了同样的错误,我也得到了同样的错误。
为了解决这个问题,我检查了字典键并注意到字典键和值被编码为字节。
所以,我使用字节密钥访问和解码到 t运行slate 消息到 str,如:
import email
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True
## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
messages = server.search(['FROM', 'email_of_the_contact@gmail.com'])
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.items():
msg_string = data[b'RFC822']
msg = email.message_from_string(msg_string.decode())
print('ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date']))