Python3输出到html写文件需注意,出现前缀b'

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weizhixiang/article/details/51043412

    def output_html(self):
        fout=open('output.html','w',encoding='UTF-8', newline='')
        fout.write('<html>')
        fout.write('<head>')
        fout.write('<meta charset = "UTF-8"/>')
        fout.write('</head>')
        fout.write('<body>')
        fout.write('<table>')

        for data in self.datas:
            fout.write('<tr>')
            fout.write('<td>%s</td>' % data['url'])
            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>")

要注意open方法,还有指定网页的编码格式为utf-8,否则会出现字符串b前缀和编码错误


UnicodeEncodeError: 'gbk' codec can't encode character '\u02c8'



猜你喜欢

转载自blog.csdn.net/weizhixiang/article/details/51043412