UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
程序员文章站
2022-06-22 12:55:21
Scrapy爬虫向数据库写入数据时报错: UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) 解决方案: 1.我使用的是SQLALchemy,在cr ......
scrapy爬虫向数据库写入数据时报错:
unicodeencodeerror:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
解决方案:
1.我使用的是sqlalchemy,在create_engine()中添加charset=utf8和encoding='utf-8’
engine = create_engine(mysql+mysqldb://”root:123456@127.0.0.1:3306/test?charset=utf8”,encoding=”utf-8”)
2.修改mysql配置文件
1.获取root权限
sudo -i
2.进入配置文件所在路径
cd /etc/mysql/mysql.conf.d
3.备份
cp -p mysqld.cnf mysqld.cnf.bak
4.用vim打开mysqld.cnf,添加以下内容
mysqld character_set_server = utf8
5.重新加载配置文件
/etc/init.d/mysql reload
下一篇: python学习-1 编程语言的介绍
推荐阅读
-
UnicodeEncodeError: 'ascii' codec can't encode characters in position
-
UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
-
macOS解决sublime text3运行python3报:UnicodeEncodeError: 'ascii' codec can't encode characters in position
-
UnicodeEncodeError: 'ascii' codec can't encode characters in position
-
Python3 requests post 中文乱码 UnicodeEncodeError: ‘latin-1‘ codec can`t encode characters in ……
-
UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
-
Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)