前提・実現したいこと
Anacondaをインストールし、
spyderでwindows上にPython環境を作り、Seleniumで
スクレイピングのソースコードがあります。
※プログラムに関しては全くの初心者です。
コードや環境は他人に用意してもらいましたが、
エラーが出て、うまく稼働しなく困っています。
プログラムの内容は、スクレイピングで
Gmail APIを利用していて、
10秒毎にGmailアカウントを閲覧し、メールの件名に、
指定した文字列のメール受信があった場合は、
自動的にブラウザを起動して、証券会社の口座へログインし、
特定のブラウザ操作を行うというものです。
しかし起動して、放置しておくと、その日時にメール受信がないのに、
プログラムが勝手に反応してしまいます。
時間を照らし合わせると、過去に受信したメールの受信時刻に反応してしまっているようです。
以下コードに間違った記述がないかなど、
有識者の方、ご教授いただければと存じます。
該当のソースコード
# -*- coding: utf-8 -*- #!/usr/bin/python """ Created on Wed Apr 17 23:24:27 2018 @author: Starwars """ 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 from __future__ import print_function 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) #%% # 1つの注文を取り消しして、指定した枚数分だけ成行決済します。 # 1、枚数を指定してください。2、分割エントリーの場合は[建玉明細を指定する]をクリックを有効に print(datetime.today().strftime("%Y/%m/%d %H:%M:%S"),"先物自動決済システムを起動しました") 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: Trend Beam' and delta.seconds<60: print(datetime.today().strftime("%Y/%m/%d %H:%M:%S"),"ただ今、MT4より売買指示のメールを受信しました") 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"),"これより先物取引の決済処理を開始します") user_id="00123456" user_password="XXXXXXXXXXX" temp_path=r"C:\Users\masa\Documents\chromedriver.exe" numbers="2" driver = webdriver.Chrome(temp_path) driver.implicitly_wait(60) driver.get("https://s10.kabu.co.jp/_mem_bin/members/login.asp?/Members/") driver.find_element_by_name("SsLogonUser").send_keys(user_id) driver.find_element_by_name("SsLogonPassword").send_keys(user_password) driver.find_element_by_name("image1").click() driver.implicitly_wait(60) driver.find_element_by_name("nav_g_02").click() driver.implicitly_wait(60) #「先物タブ」をクリック driver.find_element_by_xpath('/html/body/table[3]/tbody/tr[1]/td[1]/table/tbody/tr/td[13]/a/div').click() driver.implicitly_wait(60) #「取り消し訂正タブ」をクリック driver.find_element_by_xpath('/html/body/table[4]/tbody/tr[1]/td[1]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/table/tbody/tr/td/a[3]/img').click() driver.implicitly_wait(60) #「取消」をクリック driver.find_element_by_xpath('//*[@id="myTable"]/tbody/tr/td[11]/a[1]/img').click() #「取り消しを行う」をクリック driver.find_element_by_xpath('/html/body/table[4]/tbody/tr[1]/td[1]/table/tbody/tr[2]/td/div[2]/form/table/tbody/tr/td/table/tbody/tr/td[3]/input[1]').click() #「お取引タブ」をクリック driver.find_element_by_xpath('/html/body/table[2]/tbody/tr/td/a[2]/img').click() #「先物タブ」をクリック driver.find_element_by_xpath('/html/body/table[3]/tbody/tr[1]/td[1]/table/tbody/tr/td[13]/a/div').click() #「返済注文タブ」をクリック driver.find_element_by_xpath('/html/body/table[4]/tbody/tr[1]/td[1]/table/tbody/tr[1]/td/table/tbody/tr[2]/td/table/tbody/tr/td/a[2]/img').click() #「返済」をクリック driver.find_element_by_xpath('//*[@id="pos001"]/td[9]/a[1]/img').click() #[建玉明細を指定する]をクリック driver.find_element_by_xpath("//*[@id='SelectPositionDetailFlag']").click() #「数量」に1を入力 driver.find_element_by_name("Qty").send_keys(numbers) #「成行マーケットオーダー」 をクリック driver.find_element_by_xpath('/html/body/table[4]/tbody/tr[1]/td[1]/table/tbody/tr[2]/td/form/div/table[5]/tbody/tr/td/table/tbody/tr/td[1]/table[1]/tbody/tr[3]/td[2]/table[2]/tbody/tr/td[1]/label').click() #「確認画面へ」をクリック driver.find_element_by_xpath('/html/body/table[4]/tbody/tr[1]/td[1]/table/tbody/tr[2]/td/form/div/table[5]/tbody/tr/td/table/tbody/tr/td[1]/table[2]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/input').click() #「注文する」をクリック driver.find_element_by_xpath('//*[@id="form2"]/div/table/tbody/tr/td/table[4]/tbody/tr/td/table/tbody/tr/td[3]/input[1]').click() time.sleep(30) #driver.close()
現状の打開策
今日のメール受信時刻には、明日反応してしまうので、仕方なく、毎回Gmailを削除してから稼働させています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/14 01:10