问题:
我有以下代码,可以刮取数据,但不能插入到mysql。它不断给我以下错误信息: mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
##代码:
这是我的python脚本。
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()
我认为这与:
sql = "UPDATE cpu set family = ? where name = ?"
mycursor.executemany(sql, list(zip(_titles, _cpus)))
其他:
如果你需要更多的信息,请告诉我
谢谢
1条答案
按热度按时间46scxncf1#
这只是sql语句的一个小错误,因为库找不到将数据放入的位置。只需要改变
sql = "UPDATE cpu set family = ? where name = ?"
至sql = "UPDATE cpu set family = %s where name = %s"