使用 gmail python API 如何获取没有标签的最新电子邮件 "read"
Using the gmail python API how can I get the most recent email that does not have a label "read"
results = service.users().messages().list(userId='me', labelIds=['Label_8507504117657095973'], maxResults=1).execute()
此代码段获取具有任意标签“已读”的最新电子邮件。我如何调整它以获得 没有 具有此标签的最新电子邮件?
results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973'], maxResults=1).execute()
将 =
替换为 !=
由于某种原因无法返回错误:
results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973''], maxResults=1).execute() SyntaxError: positional argument follows keyword argument
答案:
除了使用 labelIds
参数,您还可以使用 Gmail 搜索运算符进行查询。
更多信息:
根据 Gmail search operators 上的文档:
You can use words or symbols called search operators to filter your Gmail search results. You can also combine operators to filter your results even more.
What you can search by
Search operator & example
Messages that have a certain label
label:
Example: label:friends
因此,与其在 labelIds
参数中使用标签 ID Label_8507504117657095973
,不如在 q
参数中使用上述搜索词来删除 没有有标签:
labelName = "yourLabelName"
results = service.users()
.messages()
.list(userId='me', q="-label:"+labelName, maxResults=1)
.execute()
这将 return 没有给定标签的单个结果:
{
"messages": [
{
"id": "XXXXXXXXXXXXXXXX",
"threadId": "YYYYYYYYYYYYYYYY"
}
],
"nextPageToken": "1309905752354234521",
"resultSizeEstimate": 1
}
参考文献:
results = service.users().messages().list(userId='me', labelIds=['Label_8507504117657095973'], maxResults=1).execute()
此代码段获取具有任意标签“已读”的最新电子邮件。我如何调整它以获得 没有 具有此标签的最新电子邮件?
results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973'], maxResults=1).execute()
将 =
替换为 !=
由于某种原因无法返回错误:
results = service.users().messages().list(userId='me', labelIds!=['Label_8507504117657095973''], maxResults=1).execute() SyntaxError: positional argument follows keyword argument
答案:
除了使用 labelIds
参数,您还可以使用 Gmail 搜索运算符进行查询。
更多信息:
根据 Gmail search operators 上的文档:
You can use words or symbols called search operators to filter your Gmail search results. You can also combine operators to filter your results even more.
What you can search by Search operator & example Messages that have a certain label label:
Example: label:friends
因此,与其在 labelIds
参数中使用标签 ID Label_8507504117657095973
,不如在 q
参数中使用上述搜索词来删除 没有有标签:
labelName = "yourLabelName"
results = service.users()
.messages()
.list(userId='me', q="-label:"+labelName, maxResults=1)
.execute()
这将 return 没有给定标签的单个结果:
{
"messages": [
{
"id": "XXXXXXXXXXXXXXXX",
"threadId": "YYYYYYYYYYYYYYYY"
}
],
"nextPageToken": "1309905752354234521",
"resultSizeEstimate": 1
}