今天用python(版本3.5)将爬取的数据写入html文件中,代码以下:html
fout=open('output1.html','w',encoding='utf-8') fout.write("<html>") fout.write("<body>") fout.write("<table>") for data in self.datas: fout.write("<tr>") #print(data['summary']) fout.write("<td>%s</td>"%data['url']) #print(data['title']) fout.write("<td>%s</td>" % data['title']) fout.write("<td>%s</td>" % data['summary']) fout.write("</tr>") fout.write("</table>") fout.write("</body>") fout.write("</html>")
发现数据在控制台输出正常,以下图:python
然而在edge浏览器打开时,中文部分全是乱码。以下图:浏览器
经过查询资料后,得到正确的解决方案为在html和body之间插入一句:编码
fout.write("<meta charset=\"utf-8\">")告诉浏览器打开文件的编码方式
以下所示
fout=open('output.html','w',encoding='utf-8') fout.write("<html>") fout.write("<meta charset=\"utf-8\">") fout.write("<body>") fout.write("<table>")
参考博客连接:https://www.cnblogs.com/nx520zj/p/5865607.htmlurl