爬虫4 html输出器 html_outputer.py

#coding:utf8
__author__ = 'wang'


class HtmlOutputer(object):

    def __init__(self):
        self.datas = [];

    def collect_data(self, data):
        if data is None:
            return
        print data
        self.datas.append(data)

    def output_html(self):
        fout = open('output.html', 'w')
        fout.write('<html>')
        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'].encode('utf-8'))
            fout.write('<td>%s</td>' % data['summary'].encode('utf-8'))
            fout.write('</tr>')

        fout.write('</table>')
        fout.write('</body>')
        fout.write('</html>')

    def test(self):
        pass

 

posted @ 2016-11-29 22:45  brady-wang  阅读(460)  评论(0编辑  收藏  举报