Guest User

VMR

a guest
Mar 31st, 2010
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. '''
  2. Created on Mar 31, 2010
  3.  
  4. @author: Damjan
  5. '''
  6.  
  7. #!/usr/bin/env python
  8. # -*- coding: utf-8 -*-
  9.  
  10. import urllib2, re
  11. from xml.dom import minidom
  12.  
  13. class RssReader():
  14.     def __init__(self, url):
  15.         self.xml = urllib2.urlopen(url)
  16.         self.title = []
  17.         self.links = []
  18.         self.description = []
  19.        
  20.         if (self.xml):
  21.             xmldoc = minidom.parse(self.xml)
  22.             if (xmldoc):
  23.                 rootNode = xmldoc.documentElement
  24.                 for node in rootNode.childNodes:
  25.                     if (node.nodeName == "channel"):
  26.                         for item_node in node.childNodes:
  27.                             for item in item_node.childNodes:
  28.                        
  29.                                 if (item.nodeName == "title"):
  30.                                     title = ""
  31.                                     for text_node in item.childNodes:
  32.                                         if (text_node.nodeType == node.TEXT_NODE):
  33.                                             title += text_node.nodeValue
  34.                                         if (len(title)>0):
  35.                                             self.title.append(title)
  36.  
  37.                                 if (item.nodeName == "description"):
  38.                                     description = ""
  39.                                     for text_node in item.childNodes:
  40.                                         if (text_node.nodeType == node.TEXT_NODE):
  41.                                             description += text_node.nodeValue
  42.                                         if (len(description)>0):
  43.                                             self.description.append(self.remove_html_tags(description))
  44.                                         else:
  45.                                             self.description.append("")
  46.  
  47.                                 if (item.nodeName == "link"):
  48.                                     link = ""
  49.                                     for text_node in item.childNodes:
  50.                                         if (text_node.nodeType == node.TEXT_NODE):
  51.                                             link += text_node.nodeValue
  52.                                         if (len(link)>0):
  53.                                             self.links.append(link)
  54.                                            
  55.  
  56.             else:
  57.                 print "Error getting XML document!"
  58.         else:
  59.             print "Error! Getting URL"
  60.  
  61.        
  62.     def remove_extra_spaces(self, data):
  63.         p = re.compile(r'\s+')
  64.         return p.sub(' ', data)
  65.    
  66.     def remove_html_tags(self, data):
  67.         p = re.compile(r'<.*?>')
  68.         return self.remove_extra_spaces(p.sub('', data))
  69.    
  70. R = RssReader("http://rss.cnn.com/rss/cnn_topstories.rss")
  71.  
  72. f = open("file.html", "w")
  73. f.write('<html><body>')
  74. i=0
  75. print len(R.title)
  76. print len(R.links)
  77. print len(R.description)
  78.  
  79. for ttl in R.title:
  80.     f.write('<a href="' + R.links[i] + '">' + ttl + '</a><p>' + R.description[i] + '</p>')
  81.     i+=1
  82. f.write('</body></html>')
  83. f.close()
Advertisement
Add Comment
Please, Sign In to add comment