Python實(shí)現(xiàn)的RSS閱讀器實(shí)例
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 14:34:31
Python實(shí)現(xiàn)的RSS閱讀器實(shí)例
Python實(shí)現(xiàn)的RSS閱讀器實(shí)例:本文實(shí)例講述了Python實(shí)現(xiàn)的RSS閱讀器。分享給大家供大家參考。具體如下: # -*- coding:utf-8 -*- # file: pyRSS.py # import Tkinter import urllib import xml.parsers.expat class MyXML: # XML解析類 def _
導(dǎo)讀Python實(shí)現(xiàn)的RSS閱讀器實(shí)例:本文實(shí)例講述了Python實(shí)現(xiàn)的RSS閱讀器。分享給大家供大家參考。具體如下: # -*- coding:utf-8 -*- # file: pyRSS.py # import Tkinter import urllib import xml.parsers.expat class MyXML: # XML解析類 def _
本文實(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,
'******************************
')
self.edit.insert(Tkinter.END, 'Title: ')
self.edit.insert(Tkinter.END, data + '
')
elif self.description:
self.edit.insert(Tkinter.END, 'Date: ')
self.edit.insert(Tkinter.END, data + '
')
elif self.date:
self.edit.insert(Tkinter.END, 'Date: ')
self.edit.insert(Tkinter.END, data + '
')
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) # 打開(kāi)URL
data = page.read() # 讀取URL內(nèi)容
parser = MyXML(self.edit) # 生成實(shí)例對(duì)象
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()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
Python實(shí)現(xiàn)的RSS閱讀器實(shí)例
Python實(shí)現(xiàn)的RSS閱讀器實(shí)例:本文實(shí)例講述了Python實(shí)現(xiàn)的RSS閱讀器。分享給大家供大家參考。具體如下: # -*- coding:utf-8 -*- # file: pyRSS.py # import Tkinter import urllib import xml.parsers.expat class MyXML: # XML解析類 def _