0
我正在編寫一個腳本來通過我的電子郵件來計算我在Uber遊樂設施上花費了多少錢。 (優步向您發送收據給您的電子郵件,其中包括費用,我正在通過電子郵件查找費用,然後將其添加到陣列中)我使用1封電子郵件進行測試,但遇到問題同時試圖循環所有的電子郵件。使用imaplib獲取錯誤
我知道id_list
(電子郵件ID列表)是一個完整的數組。當我把它打印出來,我得到:['4726', '5543', '5587', '5589', '5661', '5758', '5759', '5853', '5986', '6071', '6072', '6076', '6105', '6141', '6229']
這裏是我完整的錯誤回溯:
Traceback (most recent call last):
File "/Users/Harrison/Desktop/Uber/Uber.py", line 22, in <module>
result,data = mail.fetch(id, "(RFC822")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 456, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 1088, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 918, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: FETCH command error: BAD ['Could not parse command']
這裏是我的代碼:
import imaplib
import email
from bs4 import BeautifulSoup
final_cost1 = ""
final_cost2 = ""
cost_array = []
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('my email', 'my passowrd')
mail.list()
mail.select('inbox')
result,data = mail.search(None, 'FROM', '"Uber Receipts"')
ids = data[0]
id_list = ids.split()
for id in id_list:
result,data = mail.fetch(id, "(RFC822")
message_body = data[0][1]
uber_email = email.message_from_string(message_body)
for part in uber_email.walk():
if part.get_content_type() == "text/html":
body = part.get_payload(None, decode=True)
soup = BeautifulSoup(body, 'html.parser')
#print soup.prettify()
for row in soup.find_all('td', attrs={"class" : "price final-charge"}):
final_cost1 = row.text.lstrip().strip()
for row in soup.find_all('td', attrs={"class" : "totalPrice chargedFare black"}):
final_cost2 = row.text.lstrip().strip()
if final_cost1 != "":
print final_cost1
cost_array.append(final_cost1)
if final_cost2 != "":
print final_cost2
cost_array.append(final_cost2)
print cost_array