I've written some code to scrape two pieces of data from a website:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
from numpy import nan
import mysql.connector
cnx = mysql.connector.connect(user='root', password='*mypassword*',
host='127.0.0.1',
database='racing')
#cnx.close()
cursor = cnx.cursor()
driver = webdriver.Chrome("/anaconda3/chromedriver")
driver.get("https://www.racingpost.com/results/2018-03-20")
timeout=10
expand = driver.find_element_by_xpath('/html/body/div[3]/div[1]/main/div/div/div/div/a[2]').click()
#all race results
races_element = driver.find_elements_by_class_name('rp-timeView__raceName')
races = []
for x in races_element:
races.append(x.text)
time_element = driver.find_elements_by_class_name('rp-timeView__time')
times = []
for x in time_element:
times.append(x.text)
for race, time in zip(races,times):
print(race + ': ' + time)
cursor.executemany("INSERT INTO racecard (course) VALUES (%s)", races)
driver.quit()
The code is successfully printing all the results on the page as 'coursename: time' but I'm unsure how I take that output and enter it into a single table on multiple rows with 1 column for 'coursename' and the second for 'time'.
The cursor line I entered was an attempt to enter the races list only but it hasn't worked.
Can anyone advise whether I should try output the 'races' and 'times' lists to the database or via the printed zip pair and in each situation, what code to use?
Thanks