import pandas as pd
import datetime as dt
import sys
from urllib import request
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep


url = r"https://www.keiba.go.jp/"
dp = r"C:\driver\94\chromedriver.exe"
options = Options()
driver = webdriver.Chrome(dp, options=options)
driver.get(url)
sleep(15)
driver.find_element_by_xpath('//a[p[contains(text(),"開催日程")]]').click()
driver.switch_to_window(driver.window_handles[1])
td=driver.find_elements_by_tag_name("td")[1]
ancs=td.find_elements_by_tag_name('a')

anc = ancs[0]
anc.click()
rt=driver.find_element_by_class_name('raceTable')
rt2=rt.find_elements_by_class_name('data')
from operator import itemgetter
f = itemgetter(0,1,4,5)
# f2 = itemgetter(0,1,2,3)
for rt3 in rt2:
    tds=rt3.find_elements_by_tag_name('td')
    if len(tds) > 9:
        ff=f(tds)
        for td in ff:
            td.text
        ff[2].find_element_by_tag_name('a').click()

ps = driver.page_source
soup = BeautifulSoup(ps,"html.parser")

soup.find('ul',class_='dataArea').find('li').text.replace('\u3000',' ')
CT=soup.find('section',class_='cardTable')
tBorders=CT.findAll('tr',class_='tBorder')
len(tBorders) # 出走頭数

tes=pd.read_html(driver.current_url)

for tBorder in tBorders:
    for trr in tBorder.findAll('tr'):
        for tdd in trr.findAll('td'):
            tdd.text
td class="horseNum"
a class="horseName"

for td in tds:
  td.text
  race = td.find_elements_by_tag_name('a')
  if len(race) > 0:
   race[0].click()

horses = driver.find_elements_by_class_name('horseName')
horses[0].click()
driver.switch_to_window(driver.window_handles[2])
ps = driver.page_source
soup = BeautifulSoup(ps,"html.parser")
tb = pd.read_html(ps)
