Anacondaをインストールし、
spyderでwindows上にPython環境を作り、Seleniumで
スクレイピングのソースコードがあります。
前提・実現したいこと
「#%%」で2分割に区切って、
ctrl+enter を2回して毎回稼働していますが、
「#%%」を削除して、1つのファイルとして実行するとエラーが出てしまいます。
どのように修正すればよいか、
有識者の方、ご教授いただければと存じます。
どうぞよろしくお願い致します。
発生している問題・エラーメッセージ
runfile('C:/Users/masa/Documents/Auto_Trading_System_exit_1.py', wdir='C:/Users/masa/Documents') Traceback (most recent call last): File "C:\Users\masa\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-1-946430cb604a>", line 1, in <module> runfile('C:/Users/masa/Documents/Auto_Trading_System_exit_1.py', wdir='C:/Users/masa/Documents') File "C:\Users\masa\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\Users\masa\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/masa/Documents/Auto_Trading_System_exit_1.py", line 22 from __future__ import print_function ^ SyntaxError: from __future__ imports must occur at the beginning of the file
該当のソースコード
# -*- 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="ATmasXXXXXXX" 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()
試したこと
51行目に「#%%」で区切るとちゃんと稼働しますが、毎回なので面倒です・・・
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/13 14:10