0

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

1 Answer 1

1

Assuming that your rest of the code works well and all you need is just to update the obtained values into the database , the first thing you need to do is to create a table before the for loop ,

cursor.execute("CREATE TABLE <table-name>(coursename varchar(255),time varchar(255));")

Once you've got the table ready , you can insert the values into the table, just by adding an INSERT statement inside the for loop where you are printing the values now

for race, time in zip(races,times):
    print(race + ': ' + time)   
    cursor.execute("INSERT INTO <table-name> VALUES(%s,%s);,(race,time)")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. The table created succesfully but the rest of the code returns: ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s);,(race,time)' at line 1
Solved it with this: for race, time in zip(races,times): print(race + time) cursor.execute("""INSERT INTO racecard (coursename,time) VALUES (%s,%s)""", (race,time)) cnx.commit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.