美文网首页
Python基本爬虫(数据存储器)

Python基本爬虫(数据存储器)

作者: 原来不语 | 来源:发表于2017-12-12 19:51 被阅读0次
    # -*-encoding:utf-8 -*-
    import codecs
    
    class DataOutput(object):
    """docstring for DataOutput"""
    def __init__(self):
        super(DataOutput, self).__init__()
        self.datas=[]
        
    def store_data(self,data):
        if data is None:
            return 
        self.datas.append(data)
    
    def output_html(self):
        fout = codecs.open('baike.html','w',encoding='utf-8')
        fout.write("<html>")
        fout.write("<head><meta charset='utf-8'/></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>")
            self.datas.remove(data)
        fout.write("</table>")
        fout.write("</body>")
        fout.write("</html>")
        fout.close()
    

    相关文章

      网友评论

          本文标题:Python基本爬虫(数据存储器)

          本文链接:https://www.haomeiwen.com/subject/vdanixtx.html