Python-implemented RSS reader instance

Source: Internet
Author: User
This article mainly introduces the Python-implemented RSS reader. The example analyzes the skills related to XML parsing for RSS reading, which has some reference value, if you need an RSS reader, refer to the example in this article to describe the RSS reader implemented by Python. Share it with you for your reference. The details are as follows:

#-*-Coding: UTF-8-*-# file: pyRSS. py # import Tkinterimport urllibimport xml. parsers. expatclass MyXML: # XML parsing class def _ init _ (self, edit): self. parser = xml. parsers. expat. parserCreate () # generate XMLParser self. parser. startElementHandler = self. start # start mark processing method self. parser. endElementHandler = self. end # end tag processing method self. parser. characterDataHandler = self. data # character data processing method self. title = False # status flag self. description = False self. date = False self. edit = edit # multi-line text box object def start (self, name, attrs): # start flag processing method if name = 'title': # determine whether it is the title element self. title = True # Set flag to True elif name = 'description': self. description = True elif name = 'pubdate': # determines whether it is pubDate self. date = True # flag set to True else: pass def end (self, name): # end tag processing if name = 'title': self. title = False # flag set to False elif name = 'description': self. description = False elif name = 'pubdate': self. date = False # flag set to False else: pass def data (self, data): # character data processing method if self. title: # output data self according to the flag status. 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 # Create component self. entryUrl = Tkinter. entry (root, width = 30) self. entryUrl. place (x = 65, y = 15) self. get = Tkinter. button (root, text = 'read 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) # Open URL data = page. read () # read URL content parser = MyXML (self. edit) # generate the Instance Object parser. feed (data) # process XML data page. close () root = Tkinter. tk () root. title ('rss reader') window = Window (root) root. min size (700,500) root. maxsize (700,500) root. mainloop ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.