import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="user_name",
password="password",
database="amazon"
)
# カーソルを取得
cursor = conn.cursor()
#要素を取得しながらDB insert
def getInsert():
# otpCodeという名前の要素が見つかるまで1秒間待機
WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".Product")))
elements = driver.find_elements_by_class_name('Product')
for element in elements:
# 要素内のテキストを取得
# element_text = element.text
Product__title = element.find_element_by_class_name("Product__title").text
Product__priceValue = element.find_element_by_class_name("Product__priceValue").text
Product__priceValue = Product__priceValue.replace(",", "") #カンマ除去
Product__priceValue = Product__priceValue.replace("円", "")
# データ挿入のクエリ
insert_data_query = """
INSERT INTO product (productTitle, price) VALUES (%s, %s)
"""
# データ挿入
price_data = (Product__title, Product__priceValue)
cursor.execute(insert_data_query, price_data)
# 変更を確定
conn.commit()
print(Product__title)
## 要素を取得する処理 とか
getInsert() #関数実行
seleniumでMySQLにInsert
Categories:
Tags: