pymysql报错 AttributeError: 'NoneType' object has no attribute 'encoding'

问题:在用request抓取快代理做ip代理池,把ip信息存储到mysql然后报错。

Traceback (most recent call last):
  File "C:/Python/scrapy/BOSS/tools/IPproxies.py", line 5, in 
    conn = pymysql.connect(host='localhost', user='root', password='longlong86', db='ip_tools', charset='utf-8')
  File "C:\Program Files\Python37\lib\site-packages\pymysql\__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\pymysql\connections.py", line 284, in __init__
    self.encoding = charset_by_name(self.charset).encoding
AttributeError: 'NoneType' object has no attribute 'encoding'

解决:是在创建连接的时候,charset这个参数写成utf-8,应该是utf8.
所以正确的写法是

import pymysql
错误写法:
conn = pymysql.connect(host='localhost', user='root', password='root', db='表名', charset='utf-8')
正确写法:
conn = pymysql.connect(host='localhost', user='root', password='root', db='表名', charset='utf8')

你可能感兴趣的:(pymysql)