gpt4 book ai didi

database - 插入列表时python2.7 MySQLdb语法错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:34:51 29 4
gpt4 key购买 nike

我正在尝试将抓取的数据插入 MySQL 数据库中......我从互联网上找到了到目前为止我需要的所有代码,但我可以理解其中的大部分......这是我的代码:

import MySQLdb
from bs4 import BeautifulSoup
import requests
r = requests.get("http://www.metalinjection.net/")
data = r.text
soup = BeautifulSoup(data, "lxml")

news = soup.find_all("article", {"class": "post"})
titles = []
for tits in news:
titles.append(tits.contents[3].find_all("h2", {"class": "title"})
[0].text)
print titles
images = []
for image_link in news:
images.append(image_link.img['src'])
print images
for link in news:
l1 = link.a['href']

db = MySQLdb.connect(host="localhost",
user="admin",
passwd="admin",
db="lot_data")
t2 =d2 =i2 = "die"
dbc = db.cursor()
db.set_character_set('utf8')
dbc.execute('SET NAMES utf8;')
dbc.execute('SET CHARACTER SET utf8;')
dbc.execute('SET character_set_connection=utf8;')

sql = "insert into lot_data VALUES('%s', '%s', '%s')" % \
(titles, images , "nothing")
rows = dbc.execute(sql)
db.commit()
db.close()

当我运行脚本时,它给出了以下错误:

Traceback (most recent call last):
File "C:/Python27/ques.py", line 32, in <module>
rows = dbc.execute(sql)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \'The Monday Grind: MELLOW HARSHER Served Cold\', u"METALLICA\'s Former Producer Apo\' at line 1')

当我用字符串替换标题或图像时,它们可以毫无错误地插入......如果有更好、更有条理的插入方式,请指出我......

最佳答案

titlesimages 是列表。您不能以这种方式直接插入列表。 insert一次创建一行。您需要遍历列表并在每次迭代中插入一行。

如果两个列表是对应的(相同长度,相同索引上的元素属于一起)你可以重写你程序的下半部分,你插入数据库的地方,像这样:

sql = "insert into lot_data VALUES(%s, %s, %s)"
for (title, imagelink) in zip(titles, images):
data = (title, imagelink, "nothing")
dbc.execute(sql, data)

此代码遍历两个列表并一一插入元素。

编辑:优化sql语句。将 dbc.ececute(...db.commit() 之间的现有代码替换为我上面的代码。

关于database - 插入列表时python2.7 MySQLdb语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45689729/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com