class BizhiPipeline_MySQL(object):
def open_spider(self,spider):
# 创建数据库连接
self.conn = pymysql.connect(host='localhost', user='root', password='he1234', database='picture')
# 创建游标对象
self.cursor = self.conn.cursor()
def process_item(self,item,response):
global a
# sql语句
insert_sql = """
insert into ONE_PICTURE(id,content) VALUES (%s,%s)
"""
# 插入到数据库
self.cursor.execute(insert_sql,(item['a'],item['picture_content']))
# 提交数据
self.conn.commit()
def close_spider(self, spider):
# 关闭游标和连接
self.cursor.close()
self.conn.close()