论坛首页 编程语言技术论坛

python 面向对象入门 - 之 RSS

浏览 2651 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-03-01   最后修改:2010-05-28
该段代码涵盖了如下内容:
1. 类的定义,类继承
2. 类和实例的属性定义
3. 类和实例的方法定义
4. 类的可见性定义 private public protected
5. rss lib学习


'''
 python learn parse rss
 url : http://wiki.python.org/moin/RssLibraries 
 Install feedparser
 url : http://code.google.com/p/feedparser/
 dowload install file
 python setup install
''' 
import feedparser

class Inspect:    
    #print object
    def info(self, spacing=10, collapse=1):
        object = self
        """Print methods and doc strings.
        Takes module, class, list, dictionary, or string."""
        methodList = [e for e in dir(object) if callable(getattr(object, e))]
        processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
        print "\n".join(["%s %s" %
                     (method.ljust(spacing),
                      processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])

#inheirt from Inspect
class RssParser(Inspect):
    """A simple parse javaeye rss"""
    
    #Define Class atrributes
    parseCount = 0

    "initial rss parser"
    def __init__(self, url=None):
        #Define instance attributes
        self.url = url
        #Define instance protected attributes
        self._author = "edison"
        #Define instance private attributes
        self.__intro = "rss parser"
                    
    #Analysys Rss
    #instance method
    def parse(self,url):
        rss = feedparser.parse(self.url)
        self.__puts(rss)
        self.parseCount += 1
                
    #print Rss
    #instance private method
    def __puts(self,rss):
        print "info:%s" % self.__intro
        print "author:%s" % self._author       
        print "title:%s"  % rss.feed.title.encode("utf-8")
        print "subtile:%s" % rss.feed.subtitle.encode("utf-8")
        print "generator:%s" % rss.feed.generator.encode("utf-8")
        print "href:%s" % rss["href"]
        entries = rss["entries"]
        for item in entries:
            print "Rss" + "* " * 20
            e = item["summary_detail"]
            print "base:%s" % e["base"]
            print "title:%s" % e["type"]
            print "description:%s" % e["value"].encode("utf-8")

    
    @staticmethod
    def count():
        print RssParser.parseCount
    
            
if __name__ == '__main__':
    pss = RssParser("http://edisonlz.iteye.com/rss")
    print "* " * 60
    pss.parse(pss.url)
    print "* " * 60
    RssParser.count()
    print "* " * 60
    pss.info()
    pass
    #print pss method
    print dir(pss)

    

   发表时间:2010-05-19  
没有碰到编码问题么:)
0 请登录后投票
   发表时间:2010-05-27  
UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-15: ordinal not in range(128)
是有编码问题
0 请登录后投票
   发表时间:2010-05-28  
中文问题已经提交
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics