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()
scrapy中连接mysql
最新推荐文章于 2024-08-23 17:12:49 发布
本文介绍了一种使用Python的pymysql模块将爬取的图片数据存储到MySQL数据库的方法。通过实现BizhiPipeline_MySQL类,文章详细展示了如何建立数据库连接、执行插入操作并确保数据正确提交。
1065

被折叠的 条评论
为什么被折叠?



