#coding=utf-8 import urllib.request import chardet url = 'http://www.baidu.com' a = urllib.request.urlopen(url) ''' chardet模块 使用该模块可以查看字符串的编码格式:chardet.detect() ''' encode = chardet.detect(a.read()) print(encode['encoding']) #假设存在一个a.txt的文件 f = open('a.txt', 'rb') print(chardet.detect(f.read(100)))