输出打印没有乱码,插入到数据库时乱码。
数据库的编码:
piplines类的代码如下:
class KGbookmysql(object):
def process_item(self, item, spider):
'''
将爬取到的信息保存到mysql数据库
'''
#将数据从item中提取出来
title = item['title']
author = item['author']
score = item['score']
peoples = item['peoples']
#summary = item['summary']
#和本地数据库scrapydb建立连接
db = pymysql.connect(
host='localhost',
#port=3306,
user='root',
password='******',
db='scrapydb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
#获取游标
cursor = db.cursor()
#有则删除
cursor.execute('drop table if exists kgbook')
#创建表的sql语句
sql1 = '''create table kgbook(
id int primary key auto_increment,
title varchar(60) not null,
author varchar(60) not null,
score varchar(30),
peoples varchar(30)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;
'''
cursor.execute(sql1)
#db.close()
sql_insert = '''insert into kgbook(title,author,score,peoples) values(%s,%s,%s,%s)'''
try:
with db.cursor() as cursor:
cursor.execute(sql_insert, (title, author, score, peoples))
db.commit()
finally:
db.close()
return item
插入结果如下:
知道的前辈请指点错误在哪!感谢!
set names utf8;
select * from table;
还有问题再调整你客户端工具的显示字符集