浏览 2652 次
锁定老帖子 主题:python 面向对象入门 - 之 RSS
精华帖 (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) 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-05-19
没有碰到编码问题么:)
|
|
返回顶楼 | |
发表时间:2010-05-27
UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-15: ordinal not in range(128)
是有编码问题 |
|
返回顶楼 | |
发表时间:2010-05-28
中文问题已经提交
|
|
返回顶楼 | |