Selenium列表进入MySQL数据库表

时间:2018-03-23 18:13:12

标签: python mysql database selenium selenium-chromedriver

我已经编写了一些代码来从网站上抓取两个数据:

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()

代码已成功将所有结果打印在页面上,并且' coursename:time'但是我不确定我是如何获取该输出并将其输入到多行中的单个表中,其中包含1列,用于' coursename'第二个是时间'。

我输入的光标只是试图进入比赛列表,但它没有效果。

任何人都可以建议我是否应该尝试输出比赛'和'时代'列表到数据库或通过打印的zip对,在每种情况下,使用什么代码?

由于

1 个答案:

答案 0 :(得分:0)

假设您的其余代码运行良好,您只需将获取的值更新到数据库中,您需要做的第一件事就是在for循环之前创建一个表,

for race, time in zip(races,times):
    print(race + ': ' + time)   
    cursor.execute("INSERT INTO <table-name> VALUES(%s,%s);,(race,time)")

准备好表格后,您可以将值插入表格中, 只需在for循环中添加一个INSERT语句,然后在那里打印值

create table schedule(
  user_id int primary key not null,
  start_date date not null,
  end_date date not null,
  mon int not null,
  tue int not null,
  wed int not null,
  thu int not null,
  fri int not null,
  sat int not null,
  sun int not null
);

insert into schedule (user_id, start_date, end_date, 
                      mon, tue, wed, thu, fri, sat, sun) 
  values (30, '2018-03-01', '2018-03-15', 0, 1, 0, 1, 0, 0, 0);

insert into schedule (user_id, start_date, end_date, 
                      mon, tue, wed, thu, fri, sat, sun) 
  values (31, '2018-02-20', '2018-02-23', 1, 1, 1, 1, 1, 0, 0);