Python實(shí)現(xiàn)的RSS閱讀器實(shí)例
更新時(shí)間:2015年07月25日 10:37:54 作者:Sephiroth
這篇文章主要介紹了Python實(shí)現(xiàn)的RSS閱讀器,實(shí)例分析了XML解析實(shí)現(xiàn)RSS閱讀的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Python實(shí)現(xiàn)的RSS閱讀器。分享給大家供大家參考。具體如下:
# -*- coding:utf-8 -*- # file: pyRSS.py # import Tkinter import urllib import xml.parsers.expat class MyXML: # XML解析類 def __init__(self, edit): self.parser = xml.parsers.expat.ParserCreate() # 生成XMLParser self.parser.StartElementHandler = self.start # 起始標(biāo)記處理方法 self.parser.EndElementHandler = self.end # 結(jié)束標(biāo)記處理方法 self.parser.CharacterDataHandler = self.data # 字符數(shù)據(jù)處理方法 self.title = False # 狀態(tài)標(biāo)志 self.description = False self.date = False self.edit = edit # 多行文本框?qū)ο? def start(self, name, attrs): # 起始標(biāo)記處理方法 if name == 'title': # 判斷是否為title元素 self.title = True # 標(biāo)志設(shè)為真 elif name == 'description': self.description = True elif name == 'pubDate': # 判斷是否為pubDate self.date = True # 標(biāo)志設(shè)為真 else: pass def end(self, name): # 結(jié)束標(biāo)記處理 if name == 'title': self.title = False # 標(biāo)志設(shè)為假 elif name == 'description': self.description = False elif name == 'pubDate': self.date = False # 標(biāo)志設(shè)為假 else: pass def data(self,data): # 字符數(shù)據(jù)處理方法 if self.title: # 根據(jù)標(biāo)志狀態(tài)輸出數(shù)據(jù) self.edit.insert(Tkinter.END, '******************************\n') self.edit.insert(Tkinter.END, 'Title: ') self.edit.insert(Tkinter.END, data + '\n') elif self.description: self.edit.insert(Tkinter.END, 'Date: ') self.edit.insert(Tkinter.END, data + '\n') elif self.date: self.edit.insert(Tkinter.END, 'Date: ') self.edit.insert(Tkinter.END, data + '\n') else: pass def feed(self, data): self.parser.Parse(data, 0) class Window: def __init__(self, root): self.root = root # 創(chuàng)建組件 self.entryUrl = Tkinter.Entry(root,width = 30) self.entryUrl.place(x = 65, y = 15) self.get = Tkinter.Button(root, text = '讀取RSS', command = self.Get, font = ('system','10')) self.get.place(x = 350, y = 15) self.frame = Tkinter.Frame(root, bd=2) self.scrollbar = Tkinter.Scrollbar(self.frame) self.edit = Tkinter.Text(self.frame,yscrollcommand = self.scrollbar.set, width = 96, height = 32) self.scrollbar.config(command=self.edit.yview) self.edit.pack(side = Tkinter.LEFT) self.scrollbar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) self.frame.place(y = 50) def Get(self): url = self.entryUrl.get() page = urllib.urlopen(url) # 打開URL data = page.read() # 讀取URL內(nèi)容 parser = MyXML(self.edit) # 生成實(shí)例對象 parser.feed(data) # 處理XML數(shù)據(jù) page.close() root = Tkinter.Tk() root.title('RSS 閱讀器') window = Window(root) root.minsize(700,500) root.maxsize(700,500) root.mainloop()
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python+Empyrical實(shí)現(xiàn)計(jì)算風(fēng)險(xiǎn)指標(biāo)
Empyrical 是一個(gè)知名的金融風(fēng)險(xiǎn)指標(biāo)庫。它能夠用于計(jì)算年平均回報(bào)、最大回撤、Alpha值等。下面就教你如何使用 Empyrical 這個(gè)風(fēng)險(xiǎn)指標(biāo)計(jì)算神器2022-05-05Python實(shí)現(xiàn)繁體轉(zhuǎn)為簡體的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)繁體轉(zhuǎn)為簡體的方法,涉及Python編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-12-12django ListView的使用 ListView中獲取url中的參數(shù)值方式
這篇文章主要介紹了django ListView的使用 ListView中獲取url中的參數(shù)值方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python操作MySQL數(shù)據(jù)庫的兩種方式實(shí)例分析【pymysql和pandas】
這篇文章主要介紹了Python操作MySQL數(shù)據(jù)庫的兩種方式,結(jié)合實(shí)例形式分析了Python使用pymysql和pandas模塊進(jìn)行mysql數(shù)據(jù)庫的連接、增刪改查等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03python switch 實(shí)現(xiàn)多分支選擇功能
這篇文章主要介紹了python switch 實(shí)現(xiàn)多分支選擇功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12