【问题标题】:Fastest way to get emails from Gmail and write them into file从 Gmail 获取电子邮件并将其写入文件的最快方法
【发布时间】:2021-03-03 21:04:04
【问题描述】:

我正在编写一个脚本,它从我的 gmail 收件箱中获取 n 封电子邮件并将 n 个主题写入一个文本文件。虽然这目前工作正常。我正在寻找一种方法来获取例如 20 封 JSON 格式的电子邮件,只需要一次调用,而不是在一个循环中一个接一个地进行。

目前我有这个:

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() 在循环中执行。

非常感谢

【问题讨论】:

  • @DalmTo 代码在这里甚至不相关,我只是想知道是否有一种方法可以通过一次调用获取例如 20 封 JSON 格式的电子邮件,而不是在一个循环。

标签: python google-api gmail gmail-api google-api-python-client


【解决方案1】:

我只是想知道是否有一种方法可以通过一个电话获取例如 20 封 JSON 格式的电子邮件。

如果您查看 gmail api 的文档,您会发现只有一种方法可以返回有关电子邮件的详细信息,即 Messages.get。 Message get 将单个消息 id 作为参数并返回有关该单个消息的信息。

无法将多个消息 ID 发送到 message.get。

如果您正在寻找减少网络流量的方法,您应该查看batching 请求,该请求允许您发送多达 100 条消息。进入单个 http 请求。

您仍然需要为您批量发送的每个请求支付配额费用。

【讨论】:

  • 您好,感谢您的回复,非常感谢。我对批处理进行了一些研究,并设法制作了一个更快的版本。祝你有美好的一天
猜你喜欢
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 2021-01-09
相关资源
最近更新 更多