zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

爬取小说存入数据库

2023-03-20 15:31:01 时间

准备工作

安装:

# 数据库驱动
pip install pymysql
# 数据库连接池
pip install DBUtils

建表

CREATE TABLE `novel` (
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
   `title` varchar(100) NOT NULL COMMENT '标题',
   `content` text NOT NULL COMMENT '内容',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=2353 DEFAULT CHARSET=utf8

脚本

# 写入数据库
def write_db(chapter, content):
    sql = "INSERT INTO novel (title, content) VALUES(%(title)s, %(content)s);"
    param = {"title": chapter, "content": content}
    mysql.insert(sql, param)