1

我遇到了一个奇怪的问题,即 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
4

0 回答 0