我正在尝试将用scrapy刮下的数据保存到mysql中。但是,我有这些问题:
不再支持 MySQLdb
. 所以,我必须使用
import pymysql pymysql.install_as_MySQLdb()
在 settings.py
文件
在Python3上 %s
已弃用,我必须使用 .
使用以下代码格式化:
def close(self, reason):
csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)
mydb = MySQLdb.connect(host='localhost',
user='demo',
passwd='123456',
db='testdb')
cursor = mydb.cursor()
csv_data = csv.reader(open(csv_file))
row_count = 0
for row in csv_data:
if row_count != 0:
cursor.execute("INSERT IGNORE INTO testtb(product, category) VALUES('{}','{}')".format(*row))
row_count += 1
mydb.commit()
cursor.close()
我有以下错误
<bound method AutorSpider.close of <AutorSpider 'autor' at 0x7f64725d29b0>>
Traceback (most recent call last):
File "/home/pc/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
result = f(*args,**kw)
File "/home/pc/.local/lib/python3.6/site-packages/pydispatch/robustapply.py", line 55, in robustApply
return receiver(*arguments,**named)
File "/home/pc/Escritorio/fpyautor/fpyautor/spiders/autor.py", line 109, in close
cursor.execute("INSERT IGNORE INTO autortb(frase, categoria) VALUES({},'{}')'".format(*row))
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 727, in _read_query_result
result.read()
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 683, in _read_packet
packet.check_error()
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/pc/.local/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'titulo del item numero 1' at line 1")
还有其他更简单/有效的方法吗?因为我保存的数据在最后的刮削任务,如果我得到更多的结果(3000条)也许这是一个问题,在未来与更大的网站?
1条答案
按热度按时间h7wcgrx31#
转义字符串可以帮助你