LoginSignup
8

More than 5 years have passed since last update.

feedlyのxmlをPythonで加工する。

Last updated at Posted at 2014-05-13

要件:feedlyで購読してるブログを一覧表示(マークダウン記法)

RSSリーダーのfeedlyで購読しているRSSを取得。

XMLファイル形式でデータの中身は下記のようになっている。

xml feedly.opml.xml

<?xml version="1.0" encoding="UTF-8"?>

<opml version="1.0">
    <head>
        <title>xxxx subscriptions in feedly Cloud</title>
    </head>
    <body>
        <outline text="game" title="game">
            <outline type="rss" text="更新情報|プレイステーション オフィシャルサイト" title="更新情報|プレイステーション オフィシャルサイト" xmlUrl="http://www.jp.playstation.com/whatsnew/whatsnew.rdf" htmlUrl="http://www.jp.playstation.com/index.html"/>
        </outline>
    </body>
</opml>

Python2.5以上(ElementTreeが標準で入ってるので)

xml_edit.py
#coding:utf-8

import xml.etree.ElementTree as ET
#xmlファイルの読み込み
tree = ET.parse('feedly.opml.xml')
root = tree.getroot()

#出力したいカテゴリ
category = 'Engineers Blog'
#findallに投げる検索対象
find_el = ".//outline[@text='%s']/outline[@type='rss']" % category

es = root.findall(find_el)
for e in es:
    #辞書型のデータが得られる。
    blog_data = e.attrib
    title = ""
    url = ""
    #データを取り出す。
    for key, value in blog_data.items():
        if key == 'title':
            title = value
        elif key == 'xmlUrl':
            url = value
    print "[%s](%s)" % (title, url)
[hoge](http://hoge/)
[fuga](http://fuga/)

Markdownで取得できたのでブログにも上げている。
https://www.karumado.com/2014/05/feedly.html

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8