imap_tools 花很长时间从电子邮件中抓取链接

imap_tools Taking Long Time to Scrape Links from Emails

我正在使用 imap_tools 从电子邮件中获取 link。电子邮件非常小,只有很少的文字、图形等。数量也不多,一天大约有 20-40 封。

收到新电子邮件后,抓取 link 需要 10 到 25 秒。这看起来很长。我原以为它会少于 2 秒,速度很重要。

铌。这是一个共享邮箱,我不能简单地获取看不见的电子邮件,因为其他用户通常会在抓取工具到达之前打开电子邮件。

谁能看出是什么问题?

import pandas as pd
from imap_tools import MailBox, AND
import re, time, datetime, os
from config import email, password

uids = []
yahooSmtpServer = "imap.mail.yahoo.com"
data = {
    'today': str(datetime.datetime.today()).split(' ')[0],
    'uids': []
    }
while True:
    while True:
        try:
            client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
            try:
                if not data['today'] == str(datetime.datetime.today()).split(' ')[0]:
                    data['today'] = str(datetime.datetime.today()).split(' ')[0]
                    data['uids'] = []
                ds = str(datetime.datetime.today()).split(' ')[0].split('-')
                msgs = client.fetch(AND(date_gte=datetime.date.today()))
                for msg in msgs:
                    links = []
                    if str(datetime.datetime.today()).split(' ')[0] == str(msg.date).split(' ')[0] and not msg.uid in data['uids']:
                        mail = msg.html
                        if 'order' in mail and not 'cancel' in mail:
                            for i in re.findall(r'(https?://[^\s]+)', mail):
                                if 'pick' in i:
                                    link = i.replace('"', "")
                                    link = link.replace('<', '>').split('>')[0]
                                    print(link)
                                    links.append(link)
                                    break
                        data['uids'].append(msg.uid)
                        scr_links = pd.DataFrame({'Links': links})
                        scr_links.to_csv('Links.csv', mode='a', header=False, index=False)
                        time.sleep(0.5)
            except Exception as e:
                print(e)
                pass
            client.logout()
            time.sleep(5)
        except Exception as e:
            print(e)
            print('sleeping for 5 sec')
            time.sleep(1)

我认为这是电子邮件服务器限制超时。

尝试查看 IMAP IDLE。

自 0.51.0 imap_tools 具有 IDLE 支持:

https://github.com/ikvk/imap_tools/releases/tag/v0.51.0