scrapy 中文乱码问题解决方案

http://www.ituring.com.cn/article/114408

最近在学习使用Python操作MySQL数据库,遇到了中文乱码问题。


一、MySQL数据库

  1. 库字符集参数:character set = utf8
  2. 表字符集参数:charset = utf8
  3. 列字符集参数:character set = utf8

二、Python文件

  1. 文件编码UTF-8 
        查看:    :set fileencoding
         改变:   :set fileencoding=utf-8
  1. 文件头添加#-*- encoding:utf-8 -*-
  2. 创建MySQL数据库连接时加上编码参数,如下例:
    conn = MySQLdb.Connection(host='localhost', user='root', passwd='123456', db='test',charset='utf8')
  3. 设置Python默认编码(非必须项)
    reload(sys)
    sys.setdefaultencoding('utf-8')

三、中文处理技巧(Unicode In Python, Completely Demystified)

  1. Decode early
  2. Unicode everywhere
  3. Encode late

四、更改mysql字符集:
      查看 
        mysql> SHOW VARIABLES LIKE 'character%'
      
  1. +--------------------------+---------------------------------+   
  2. | Variable_name | Value |   
  3. +--------------------------+---------------------------------+    
  4. | character_set_client | utf8 |   
  5. | character_set_connection | utf8 |   
  6. | character_set_database | utf8 |  
  7. | character_set_filesystem | binary |   
  8. | character_set_results | utf8 |   
  9. | character_set_server | utf8 |   
  10. | character_set_system | utf8 |   
  11. | character_sets_dir | D:"mysql-5.0.37"share"charsets" |   
  12. +--------------------------+---------------------------------+  
修改:
To set the default to UTF-8, you want to add the following to my.cnf
  [client]  
  default-character-set=utf8  
  [mysql] 
   default-character-set=utf8  
  [mysqld] 
   collation-server = utf8_unicode_ci  
  init-connect='SET NAMES utf8'  
  character-set-server = utf8 

你可能感兴趣的:(mysql,scrapy,中文,乱码)