前提・実現したいこと
Anacondaをインストールし、
spyderでwindows上にPython環境を作り、Seleniumで
スクレイピングのソースコードがあります。
※プログラムに関しては全くの初心者です。
スクレイピングでGmail APIを利用していて、
10秒毎にGmailアカウントを閲覧し、メールの件名に、
60秒以内に指定した文字列のメール受信があった場合は、
プログラムを起動させるものです。
今回はメールの件名「FW: AAAAAAAA」に加え、さらにメールの本文に「ABCD」という
特定の文字が含まれている場合と、
メール件名は見ずに、メール本文のみ参照しにいくように修正したいです。
有識者の方、ご教授いただければと存じます。
該当のソースコード
import time from datetime import datetime from datetime import date from httplib2 import Http from googleapiclient import sample_tools from apiclient import discovery from apiclient.discovery import build from oauth2client import client from oauth2client import tools from oauth2client import file from oauth2client.file import Storage from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select import dateutil.parser as parser SCOPES = 'https://www.googleapis.com/auth/gmail.readonly' store = file.Storage('C:\Users\masa\Documents\credentials-gmail.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) creds = tools.run_flow(flow, store) service = build('gmail', 'v1', http=creds.authorize(Http())) def fetch_inbox(): output=[] results = service.users().messages().list(userId='me', labelIds='INBOX', maxResults=10).execute() for msg in results['messages']: topid = msg['id'] msg = service.users().messages().get(userId='me', id=topid).execute() payld = msg['payload'] headr = payld['headers'] for k in headr: if k['name']=='Date': temp_date=(parser.parse(k['value'])).strftime("%Y-%m-%d %H:%M:%S") for j in headr: if j['name']=='From': temp_from=j['value'] for i in headr: if i['name']=='Subject': temp_subject=i['value'] output.append((temp_date, temp_from, temp_subject, msg['snippet'])) # print(msg['snippet']) return(output) judge=False while judge==False: info=fetch_inbox() for i in range(len(info)): dt1=datetime.today() dt2=datetime.strptime(info[i][0],"%Y/%m/%d %H:%M:%S") delta=dt1-dt2 if info[i][2]=='FW: AAAAAAAA' and delta.total_seconds()<60: print(datetime.today().strftime("%Y/%m/%d %H:%M:%S"),"指定のメールを受信しました") judge=True if judge==False: print(datetime.today().strftime("%Y/%m/%d %H:%M:%S"),"waiting...") time.sleep(10) if judge==True: print(datetime.today().strftime("%Y/%m/%d %H:%M:%S"),"これよりプログラムを起動します")
試したこと
if info[i][2]=='FW: AAAAAAAA' and delta.total_seconds()<60:
恐らくここの部分を修正するのだと思うのですが、どのように修正すればいいのかが、
わかりません。ご教授いただければと存じます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/30 13:41
2018/06/30 13:46 編集
2018/06/30 13:51
2018/06/30 14:07
2018/07/01 14:31
2018/07/03 09:34
2018/07/03 09:44
2018/07/03 09:48
2018/07/03 09:49
2018/07/03 09:55
2018/07/03 09:58
2018/07/03 09:58
2018/07/03 10:11
2018/07/03 10:18