【问题标题】:Python imapclient search using multiple operands使用多个操作数的 Python imapclient 搜索
【发布时间】:2018-06-26 10:53:21
【问题描述】:

我正在使用 Python 的 imapclient,我需要能够搜索多个操作数。例如,假设我想查看 2018 年 1 月 6 日或 2018 年 1 月 13 日发送的消息。我查看了 IMAP criteria with multiple ORshttps://www.limilabs.com/blog/imap-search-requires-parentheses

使用上次参考中的提示,我已经尝试过:

*r_data = M.search(['OR SENTON "6-Jan-2018"  SENTON "13-Jan-2018"'])
r_data = M.search('OR SENTON "6-Jan-2018"  SENTON "13-Jan-2018"')
r_data = M.search('SENTON "6-Jan-2018" OR SENTON "13-Jan-2018"')*

还有其他几个。每次我得到:

*imaplib.error: UID command error: BAD ['Command Argument Error. 11']*

我真的不想深入研究imapclient 代码来确定如何构造这个请求。有人有什么建议吗?

【问题讨论】:

  • 我不肯定,但我会尝试 OR (SENTON "X") (SENTON "Y")。此外,一些服务器破坏了搜索实现。那是什么服务器?
  • 您能否说明您使用的是标准库中的 imaplib 还是 IMAPClient (pypi.python.org/pypi/IMAPClient/2.0.0)?
  • 我使用的是 imapclient,而不是 imaplib。
  • Menno,不幸的是,我正在与之交谈的服务器是 Exchange。我会试试你的建议。
  • >>> data = M.search('OR (SENTON "6-Jan-2018")(SENTON "13-Jan-2018")') Traceback(最近一次通话最后):文件“”,第 1 行,在 文件中“/Users/danmahoney/anaconda/lib/python2.7/site-packages/imapclient/imapclient.py”,第 725 行,在搜索中返回 self._search(criteria , charset) raise self.error('%s command error: %s %s' % (name, typ, data)) imaplib.error: SEARCH command error: BAD ['Command Argument Error. 12'] >>>

标签: python search imap imapclient


【解决方案1】:

我遇到了同样的问题。找到了解决方案(基于comments in the source code):

r_data = M.search(['OR', 'SENTON', '6-Jan-2018', 'SENTON', '13-Jan-2018'])

或者甚至更好:

r_data = M.search(['OR', 'SENTON', date(2018, 1, 6), 'SENTON', date(2018, 1, 13)])

也适用于更复杂的查询,例如:

M.search(['OR', 'OR', 'FROM', 'a', 'FROM', 'b', 'FROM', 'c'])

【讨论】:

  • 谢谢!这很有帮助,应该会让我的生活更轻松。
【解决方案2】:

尝试使用查询生成器:

import datetime as dt
from imap_tools import AND, OR, NOT, Q, H   

# date not in the date list (NOT(date=date1 OR date=date3 OR date=date2))
q2 = NOT(OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)]))
# "NOT ((OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019))"

imap-tools

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 2021-06-17
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2013-03-07
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多