当前位置: 代码迷 >> 综合 >> scrapy----将数据保存到MySQL数据库中
  详细解决方案

scrapy----将数据保存到MySQL数据库中

热度:77   发布时间:2023-12-24 20:54:22.0

1.在pipelines.py中自定义自己的pipeline

import pymysql
class PymysqlPipeline(object):def __init__(self):# 连接数据库self.connect = pymysql.connect(host='localhost',db='bole',user='root',passwd='123456',charset='utf8',port=330,use_unicode=True)self.cursor = self.connect.cursor()def process_item(self, item, spider):cursor = self.cursorsql = 'insert into bole(title, datetime, category, content, dianzanshu, shoucanshu, pinglunshu) values (%s,%s,%s,%s,%s,%s,%s)'cursor.execute(sql, (item['title'], item['datetime'], item['category'], item['content'], item['dianzanshu'],item['shoucanshu'],item['pinglunshu']))self.connect.commit()return item

2.在settings中开启自己的pipeline

ITEM_PIPELINES = {'Bole.pipelines.PymysqlPipeline': 1,
}


  相关解决方案