Python 从Gmail获取电子邮件并将其写入文件的最快方式
我正在制作一个脚本,从我的gmail收件箱中收到n封电子邮件,并将n个主题写入一个文本文件。虽然这在目前效果很好。我正在寻找一种方法,例如只需一次调用就可以获得20封JSON格式的电子邮件,而不是在循环中逐个发送 目前,我有以下几点:Python 从Gmail获取电子邮件并将其写入文件的最快方式,python,google-api,gmail,gmail-api,google-api-python-client,Python,Google Api,Gmail,Gmail Api,Google Api Python Client,我正在制作一个脚本,从我的gmail收件箱中收到n封电子邮件,并将n个主题写入一个文本文件。虽然这在目前效果很好。我正在寻找一种方法,例如只需一次调用就可以获得20封JSON格式的电子邮件,而不是在循环中逐个发送 目前,我有以下几点: from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oau
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
resultsMessages = service.users().messages().list(userId='me', labelIds=['INBOX']).execute()
messages = resultsMessages.get('messages', [])
f = open("output.txt", "a")
message_count = int(input("How many messages do you want to write?"))
if not messages:
print("no messages found")
else:
print("messages:")
for i, message in enumerate(messages[:message_count]):
f.write("message "+ str(i))
msg = service.users().messages().get(userId='me', id=message['id']).execute()
headers = msg["payload"]["headers"]
subject = [i['value'] for i in headers if i["name"] == "Subject"]
f.write("subject: "+subject[0])
f.write("\n")
f.close()
if __name__ == '__main__':
main()
这基本上得到了100封电子邮件的ID,然后每封邮件发送一封,获取主题并将其写入文件。它工作的很好,但我想找到一个更快的方法。有没有办法只需一次呼叫就可以从服务器上收到n封JSON格式的电子邮件?我想我代码上的瓶颈是调用msg=service.users().messages().get(userId='me',id=message['id'])。execute()
在循环中执行
多谢各位
我只是想知道是否有一种方法,例如,只需一次呼叫就可以获得20封JSON格式的电子邮件
如果你查看gmail api的文档,你会发现只有一种方法可以返回邮件的详细信息,即Messages.get。Message get将单个消息id作为参数,并返回有关该单个消息的信息
无法将多个邮件ID发送到message.get
如果您正在寻找一种减少网络流量的方法,您应该查看允许您最多发送100条消息的请求。进入单个http请求
对于批量发送的每个请求,仍将向您收取配额成本
我只是想知道是否有一种方法,例如,只需一次呼叫就可以获得20封JSON格式的电子邮件
如果你查看gmail api的文档,你会发现只有一种方法可以返回邮件的详细信息,即Messages.get。Message get将单个消息id作为参数,并返回有关该单个消息的信息
无法将多个邮件ID发送到message.get
如果您正在寻找一种减少网络流量的方法,您应该查看允许您最多发送100条消息的请求。进入单个http请求
对于批量发送的每个请求,您仍将收取配额成本。@DalmTo代码在这里根本不相关,我只是想知道是否有办法只需一次调用就可以获得20封JSON格式的电子邮件,而不是在循环中逐个发送。@DalmTo代码在这里根本不相关,我只是想知道是否有一种方法可以通过一次呼叫获得20封JSON格式的电子邮件,而不是在循环中一封接一封。您好,谢谢您的回复,我非常感谢。我做了一些关于批处理的研究,我设法制作了一个更快的版本。你好,谢谢你的回复,我真的很感激。我做了一些关于批处理的研究,我设法制作了一个更快的版本。祝您今天过得愉快