|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import chardet
- def main():
- i=0
-
- with open('urls.txt','r')as f:
- urls=f.read().splitlines()
- for each_url in urls:
- response=urllib.request.urlopen(each_url)
- html=response.read()
- encode=chardet.detect(html)['encoding']
- if encode=='GB2312':
- encode='GBK'
- i+=1
- filename='url_%d.txt'%i
- with open(filename,'w',encoding=encode)as each_file:
- each_file.write(html.decode(encode,'ignore'))
- if __name__=='__main__':
- main()
复制代码
第22行是用unicode写入的,第21行encoding=encode
如果不设定encoding,默认的是unicode吧,那为啥要设定?
还有used to decode or encode the file是说输入一种code,这个文件在这种code和unicode之间随机选择吗?
本帖最后由 SixPy 于 2016-8-6 18:16 编辑
这话题,说3天3夜都说不完~
你自己去摆渡 字符集 和 字符编码
encode 是把 unicode 转换为 指定的编码格式,如 utf-8,gbk。。。。
decode 相反,是把 指定的编码格式 转换回 unicode
python3中,字符串只有 unicode一种编码, str
其他编码的只能作为 字节对象 存在。 bytes
|
|