0

Question:

I have the following code which can scrape data but not insert it into mysql. It keeps on giving me the following error message: mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

Code:

Here is my python script.

import mysql.connector
from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time, re

mydb = mysql.connector.connect(
  host="host",
  user="user",
  passwd="passwd",
  database="database"
)

mycursor = mydb.cursor()

d = webdriver.Chrome('D:/Uskompuf/Downloads/chromedriver')
d.get('https://au.pcpartpicker.com/products/cpu/overall-list/#page=1')
def cpus(_source):
  result = soup(_source, 'html.parser').find('ul', {'id':'category_content'}).find_all('li')
  _titles = list(filter(None, [(lambda x:'' if x is None else x.text)(i.find('div', {'class':'title'})) for i in result]))
  data = [list(filter(None, [re.findall('(?<=\().*?(?=\))', c.text) for c in i.find_all('div')])) for i in result]
  return _titles, [a for *_, [a] in filter(None, data)]


_titles, _cpus = cpus(d.page_source)
sql = "UPDATE cpu set family = ? where name = ?"
mycursor.executemany(sql, list(zip(_titles, _cpus)))
_last_page = soup(d.page_source, 'html.parser').find_all('a', {'href':re.compile('#page\=\d+')})[-1].text
for i in range(2, int(_last_page)+1):
   d.get(f'https://au.pcpartpicker.com/products/cpu/overall-list/#page={i}') 
   time.sleep(3)
   _titles, _cpus = cpus(d.page_source)
   sql = "UPDATE cpu set family = ? where name = ?"
   mycursor.executemany(sql, list(zip(_titles, _cpus)))

mydb.commit()

I believe it has to do with:

sql = "UPDATE cpu set family = ? where name = ?"
mycursor.executemany(sql, list(zip(_titles, _cpus)))

Other:

If you need any more information please let me know

Thanks

1 Answer 1

1

Just a small mistake about your sql statement, because library can't find where he put data into. Only need to change sql = "UPDATE cpu set family = ? where name = ?" to sql = "UPDATE cpu set family = %s where name = %s"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.