|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import chardet
- def main():
- i=0
- with(open('urls.txt')) 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 在这里会被重复定义为 1
- i +=1
- file_name = 'urls%d.txt'%i
- with open(file_name, "w",encoding = encode) as each_file:
- each_file.write(html.decode(encode, "ignore"))
- # i +=1 在这个地方进行的话
- if __name__ == '__main__':
- main()
复制代码
这个encoding =encode 保持编码一致
Python 对于编码 很看重吗 |
|