scrapy 链接数据库时报错:‘NoneType’ object has no attribute 'cursor’和ERROR: Error processing {
代码如下:
class mysqlpipelines(object):
#创建数据库链接对象
cnn = None
cursor = None#游标对象
def open_spider(self,spider):
cnn = pymysql.connect(host='127.0.0.1',port=3308,user='root',password='mysql',db='spider',charset='utf8')
def process_item(self,item,spider):
#用游标对象执行sql语句
# self.cursor = self.cnn.cursor()
self.cursor = self.cnn.cursor()
#要执行的sql语句
try:
self.cursor.execute('insert into qiushibaike values("%s,"%s","%s")'%(item["author"],item["age"],item["content"]))
self.cnn.commit()
except Exception as e:
# print(e)
self.cnn.roolback()
return item
def close_spider(self,spider):
self.cursor.close()
self.cnn.close()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201108232827616.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDQwODgzOQ==,size_16,color_FFFFFF,t_70#pic_center)
求助大佬如何解决。