我正在写一个脚本,通过我的电子邮件来计算我在优步上总共花了多少钱。(Uber给你的电子邮件发了一张收据,其中包括了费用。我正在查看电子邮件以查找成本,然后将其添加到当前的数组中),我让它使用1封电子邮件进行测试,但在试图遍历所有电子邮件时遇到了一个问题。
我知道id_list(电子邮件I列表)是一个完整的数组。当我打印出来时,我收到:['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发布于 2016-11-14 20:35:15
我犯了个愚蠢的错误。
这一行result,data = mail.fetch(id, "(RFC822")有一个错误。应该是result,data = mail.fetch(id, "(RFC822)")
https://stackoverflow.com/questions/40597215
复制相似问题