简体   繁体   English

从Python中的Gmail下载HTML

[英]Download HTML from Gmail in Python

I am attempting to download all emails from a certain sender in my gmail for analysis. 我正在尝试从gmail中的某个发件人下载所有电子邮件以进行分析。 I am running into an error on line 23. 我在第23行遇到错误。

Here is my code: 这是我的代码:

import imaplib
import email

mail = imaplib.IMAP4_SSL('imap.gmail.com')


username = raw_input('USERNAME (email):')
password = raw_input('PASSWORD: ')


try:
    mail.login(username, password)
    print "Logged in as %r !" % username
except imaplib.IMAP4.error:
    print "Log in failed."

mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

fromWho = raw_input('FROM:')

result, data = mail.uid('search', None, '(FROM fromWho )') # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]

Here is the error I am getting: 这是我得到的错误:

Traceback (most recent call last):
  File "test.py", line 23, in <module>

Any thoughts?? 有什么想法吗??

The IMAP4.uid works as command method, just using UIDs. IMAP4.uid仅作为UID用作命令方法。 The first part must be a command, that you have as search. 第一部分必须是一个搜索命令。 Hence, the other parameters should be as the ones in the search method, just remember to quoute the names in the command string. 因此,其他参数应与搜索方法中的参数相同,只记得记住命令字符串中的名称即可。

You should try something like 你应该尝试像

IMAP4.uid('SEARCH', None, '(FROM "{}")'.format(fromWho)) IMAP4.uid('SEARCH',None,'(FROM“ {}”)'。format(fromWho))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM