python写入文件乱码文图

在打开文件时启用编码格式
file = open("yue.txt",'w',encoding='utf-8')




import urllib.request
import re
data = urllib.request.urlopen('#').read().decode("utf-8")
pat ='<div class="name">(.*?)</div>'
pub = re.compile(pat).findall(data)
fh = open("yue1.txt",'w',encoding='utf-8')
for x in range(0,len(pub)):
print(pub[x])
fh.write(pub[x]+'\n')
fh.close()
posted @ 2019-07-04 22:22  端木祈月  阅读(1152)  评论(0编辑  收藏  举报