Created
January 23, 2012 06:39
-
-
Save FiNGAHOLiC/1661204 to your computer and use it in GitHub Desktop.
rssparser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| # coding: utf-8 | |
| from xml.etree.ElementTree import ElementTree | |
| from urllib import urlopen | |
| def parse_rss(url): | |
| """ | |
| RSS 2.0をパースして、辞書のリストを返す | |
| """ | |
| rss = ElementTree(file = urlopen(url)) | |
| root = rss.getroot() | |
| rsslist = [] | |
| # RSS 2.0のitemエレメントだけを抜き出す | |
| for item in[ x for x in root.getiterator() if "item" in x.tag ]: | |
| rssdict = {} | |
| for elem in item.getiterator(): | |
| for k in [ 'link', 'title', 'description', 'author', 'pubDate' ]: | |
| if k in elem.tag: | |
| rssdict[k] = elem.text | |
| else: | |
| rssdict[k] = rssdict.get(k, "N/A") | |
| rsslist.append(rssdict) | |
| return rsslist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment