每三次尝试IMAPLib获取功能失败

时间:2012-04-19 02:34:26

标签: python imaplib

我有一个奇怪的问题,即imaplib的fetch函数每三次尝试失败。我从术语列表中选择一个搜索词,然后使用该搜索词从Gmail邮箱中提取包含该词的电子邮件列表。然后我选择一条随机消息,然后拉出包含搜索词的行。我每三次运行脚本时都会收到以下错误:

Traceback (most recent call last):
File "/Users/Spike/python/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/Spike/python/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/Spike/python/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/Users/Spike/python/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1265, in full_dispatch_request
response = self.make_response(rv)
File "/Users/Spike/python/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1332, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response

我已完成一些测试并打印出电子邮件,果然,搜索字词不在那里。这意味着每三次运行脚本,它就会提取一封电子邮件,因为它包含一个实际上并不包含的搜索词。这对我来说很神秘。有谁知道到底发生了什么事?

相关代码:

clean2 = [feeling.strip() for feeling in open('feeling2.txt').readlines()] #open the txt file full of feelings from the wefeelfine api
random_feeling = random.choice(clean2) 
print "this is a random feeling", random_feeling
status, numbers_list = s.search(None, '(BODY "%s")' % random_feeling) #searching the mailbox, finding ids with that search term, saving to list
numbers.append(numbers_list) #append the numbers list

smsgids = str(numbers) #turning the numbers list into a string
ids_list = smsgids.strip().split(' ') #splitting it into a list
msg_random = random.choice(ids_list) #pulling a random id from the id list
typ, msg_data = s.fetch(msg_random, '(BODY[TEXT])') #pulling the message and saving it as a variable

soup = BeautifulSoup(repr(msg_data), "html5lib") #turning it into a beautifulsoup object, but as a string first
text = soup.find_all("div") #finding all of the html div tags and saving it as a variable
as_text = [item.text for item in text]
chat_text = '\n'.join(as_text)
print chat_text
for line in as_text:
    print random_feeling
    print line
    if random_feeling in line:
        return line

0 个答案:

没有答案