import requests import xml.etree.ElementTree as ET URL = "https://feeds.feedburner.com/TheHackersNews?format=xml" FEED_FILE = "feed.xml" def downloadRSS(): response = requests.get(URL) with open(FEED_FILE, 'wb') as file: file.write(response.content) def parseXML(xmlfile): # create element tree object tree = ET.parse(xmlfile) # get root element root = tree.getroot() # create empty list for news items newsitems = [] # iterate news items for item in root.findall('./channel/item'): # empty news dictionary news = {} # iterate child elements of item for child in item: # # special checking for namespace object content:media if child.tag == '{http://search.yahoo.com/mrss/}content': news['media'] = child.attrib['url'] else: if child.text is not None: news[child.tag] = child.text.encode('utf8') # append news dictionary to news items list newsitems.append(news) # return news items list return newsitems def saveToHTML(articles): # create news article list with open("news_scroll.html", "w") as news: news.write('\ \n\ \n\ \n\ \n\ News\n\ \n\ \n\ \n\ \n\ \n\ \n\
\n\
\n\
\n') # for each article, create an element for article in articles: news.write('\ \n\
\n\

\n\ {title}\n\

\n\

{content}...

\n\
\n\
\n\ '.format(title = article["title"].decode('utf-8'), content = article["description"].decode('utf-8'), uri = article["link"].decode('utf-8'))) news.write('\
\n\
\n\
\n\ \n\ \ ') news.write('') return def main(): print("Downloading RSS...") downloadRSS() print("RSS downloaded.") print("Parsing RSS...") articles = parseXML(FEED_FILE) print("RSS parsed into", len(articles), "articles.") print("Putting into HTML...") saveToHTML(articles) print("Put in HTML.") if __name__ == "__main__": main()