from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys
import time
import os
import configparser
import Utils
def main():
#処理時間計測
start = time.time()
printmessage('処理開始')
#コンフィグ取得
cf = configparser.ConfigParser()
cf.read("config.ini")
out_path = cf.get("Common", "OUT_PATH")
name = cf.get("Common", "ACCOUNT_NAME")
passwd = cf.get("Common", "ACCOUNT_PASS")
#初期処理
#銘柄取得
with open(os.path.join('D:\data\stock','TICKER_SYMBOL.txt'),'r') as f:
symboldatas = f.read().splitlines()
symbols = []
filenames =[]
for s in symboldatas:
tmp = s.split(',')
symbols.append(tmp[1])
filenames.append([tmp[1],tmp[0]+'_'+tmp[1]])
#日付取得
strnow = Utils.getDate(time.localtime())
#フォルダ作成
Utils.setup(symbols)
printmessage('初期処理終了')
try:
#ドライバ起動
option = webdriver.ChromeOptions()
option.add_argument('--window-size=1600,1000')
option.add_argument('--ignore-certificate-errors')
option.add_argument('--ignore-ssl-errors')
option.add_argument('--headless')
driver = webdriver.Chrome("D:\data\python\\selenium\chromedriver.exe",options=option)
driver.implicitly_wait(20)
wait = WebDriverWait(driver,10)
printmessage('ドライバ起動終了')
#ログイン
driver.get("https://finance.yahoo.com/")
driver.find_element_by_xpath('//*[@id="header-signin-link"]').click()
driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(name)
driver.find_element_by_xpath('//*[@id="login-signin"]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//*[@id="login-passwd"]').send_keys(passwd)
driver.find_element_by_xpath('//*[@id="login-signin"]').click()
time.sleep(0.5)
printmessage('ログイン完了')
#銘柄チャート画面遷移、全画面表示
driver.find_element_by_xpath('//*[@id="yfin-usr-qry"]').send_keys('U')
time.sleep(0.5)
driver.find_element_by_xpath('//*[@id="yfin-usr-qry"]').send_keys(Keys.ENTER)
time.sleep(0.5)
driver.find_element_by_link_text('Full screen').click()
time.sleep(1)
#期間設定、指標設定
driver.find_element_by_xpath('//span[contains(text(),"1Y")]/ancestor::button').click()
time.sleep(0.5)
driver.find_element_by_xpath('//span[text()="Line"]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//span[contains(text(),"Candle")]/ancestor::button').click()
time.sleep(0.5)
#移動平均線設定(50日)
driver.find_element_by_xpath('//span[text()="Indicators"]').click()
driver.find_element_by_xpath('//button[contains(text(),"Moving Average")]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').clear()
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').send_keys('50')
driver.find_element_by_xpath('//span[contains(text(),"Save")]/ancestor::button').click()
time.sleep(0.5)
#移動平均線設定(200日)
driver.find_element_by_xpath('//span[text()="Indicators"]').click()
driver.find_element_by_xpath('//button[contains(text(),"Moving Average")]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').clear()
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').send_keys('200')
driver.find_element_by_xpath('//span[contains(text(),"Save")]/ancestor::button').click()
time.sleep(0.5)
#移動平均線設定(20日)
driver.find_element_by_xpath('//span[text()="Indicators"]').click()
driver.find_element_by_xpath('//button[contains(text(),"Moving Average")]').click()
time.sleep(0.5)
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').clear()
driver.find_element_by_xpath('//table[@role="presentation"]/tbody/tr/td/input[@type="text"]').send_keys('20')
driver.find_element_by_xpath('//span[contains(text(),"Save")]/ancestor::button').click()
printmessage('指標設定完了')
#各銘柄毎スクリーンショット
#filenames : [0]ティッカーシンボル、[1]優先度+ティッカーシンボル
for symbol in filenames:
driver.find_element_by_name('s').send_keys(symbol[0])
time.sleep(1)
#driver.find_element_by_xpath('//*[@data-icon="search"]/ancestor::button').click()
driver.find_element_by_name('s').send_keys(Keys.ENTER)
time.sleep(3)
WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)
time.sleep(3)
#if driver.find_element_by_xpath('//div[@class="stx_jump_today home"]'):
# if len(driver.find_element_by_xpath('//div[@class="stx_jump_today home"]'))>0:
# driver.find_element_by_xpath('//div[@class="stx_jump_today home"]').click()
# time.sleep(0.5)
driver.find_element_by_name('s').clear()
filename = strnow + '_' + symbol[1] + '.png'
symboloutfile = os.path.join(out_path,'symbol',symbol[0],filename)
datefile = os.path.join(out_path,'date',strnow,filename)
driver.get_screenshot_as_file(symboloutfile)
driver.get_screenshot_as_file(datefile)
printmessage(symbol[0] + '取得完了')
print(str(len(filenames)) + '件の銘柄取得、正常終了しました。')
except Exception as e:
print('異常終了しました。')
finally:
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
driver.quit()
def printmessage(str):
print("*"*20)
print(str.center(20))
print("*"*20)
if __name__ == "__main__":
main()
カテゴリー