def __init__(self):
    print('连接到数据库...')
    self.connect = pymysql.connect(
        host='localhost',
        port=3306,
        db='cj',
        user='root',
        passwd='123456',
        charset='utf8',
        use_unicode=True)
    print('连接成功!')

    # 通过cur执行增删查改
    self.cur = self.connect.cursor()
    # 建表
    # self.cur.execute("CREATE TABLE cjw(id INT AUTO_INCREMENT PRIMARY KEY,"
    #                  "title TEXT CHARACTER SET utf8 COLLATE utf8_general_ci,"
    #                  "time VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci,"
    #                  "source VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci)"
    #                  " CHARACTER SET utf8 COLLATE utf8_general_ci")
    # self.connect.commit()

def process_item(self, item, spider):
    try:
        # 插入数据
        sql = "INSERT INTO cjw(title,`time`,`source`" \
              ") VALUES (%s,%s,%s)"

        value = (item['title'], item['time'], item['source'])

        self.cur.execute(sql, value)

        # 提交sql语句
        self.connect.commit()
    except Exception as error:
        # 发生错误回滚
        self.connect.rollback()
        # 出现错误时打印错误日志
        logging.error(error)

def close_spider(self, spider):
    self.cur.close()
    self.connect.close()
最后修改:2023 年 06 月 13 日
如果觉得我的文章对你有用,请随意赞赏