先日インストールした feedparser を使って RSS フィードの情報を取得して表示させてみます。他の blog などを拝見して見よう見まねで書いてみたのが以下のコード。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #coding: utf-8 import feedparser rssurl = "http://osanai.org/anyway/feed" fdp = feedparser.parse(rssurl) e = fdp.entries[0] print e.links print e.title print e.category for entry in fdp['entries']: published = entry['published'] title = entry['title'] link = entry['link'] print "published:", published print "title:", title print "link:", link, "\n" |
この blog のフィードから取得してみます。feedparser.parse( 対象 URL).entries[] のかたちでリストとしてフィードの中身が得られるようです。entries[0] で最新のエントリ、entries[1] で一つ前ですね。
上記の実行結果は下記のようになりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $ python fdp1.py [{'href': u'http://osanai.org/anyway/archives/2012/05/14235002.html', 'type': u'text/html', 'rel': u'alternate'}] Node.js を触ってみる Programming published: Mon, 14 May 2012 14:50:02 +0000 title: Node.js を触ってみる link: http://osanai.org/anyway/archives/2012/05/14235002.html published: Sun, 13 May 2012 15:11:13 +0000 title: Python でフィードを扱う(準備段階) link: http://osanai.org/anyway/archives/2012/05/14001113.html published: Sat, 12 May 2012 15:32:43 +0000 title: 土曜日にやるルーティンを定めよう link: http://osanai.org/anyway/archives/2012/05/13003243.html published: Fri, 11 May 2012 14:56:09 +0000 title: 現状に満足しない姿勢 link: http://osanai.org/anyway/archives/2012/05/11235609.html published: Thu, 10 May 2012 15:11:18 +0000 title: 目線を遠くに、そして近くに link: http://osanai.org/anyway/archives/2012/05/11001118.html |
print e.links のところはどういうことになっているのか、よくわかりませんが、複数形ではなく print e.link とやると単純に URL だけが得られました。
published でエントリが書かれた日時を取得できるようですね。でもタイムゾーンが日本ではない模様。日本時間で取得する、あるいは変換するのはどうしたらよいのかな。
次はちゃんと公式ドキュメントを読んでみたいと思います。


