簡易RSSをPythonで自作した。
GoogleのRSSリーダーを一時使用していたのだけど、サービスが停止してからRSSリーダーを一切使わなくなった。
いつか自作しようと思っていたのだけど、そのいつかは訪れることなく今の今まで来てしまったので、プログラミングの学習がてら簡易なリーダーを自作してみた。
使用したもの
簡単な説明
取得したいURLを記載したテキストファイルを準備して、それを読み込みparseして、タイトルとリンクをのみを取得し、htmlファイルへと出力する簡易な仕組み。
ソース
メイン的な。
#!/usr/bin/python # -*- coding: utf-8 -*- import feedparser, file_handle # html template html = """ <p> %s </p> <a href=%s> %s </a> """ # file 操作 file_input = file_handle.open_file("url_list.txt") file_output = file_handle.make_file() #main処理 feedparserを利用して、タイトルとurlをhtmlファイルに出力する。 for line in file_input: res = feedparser.parse(line) for entry in res.entries: title = entry.title.encode('utf-8') link = entry.link.encode('utf-8') file_output.write(html % (title, link, link))
ファイル操作するやつ。
def open_file(path): return open(path, 'r') def make_file(): import datetime today_html = "./rss_html/" + str(datetime.date.today()) + ".html" return open(today_html, "a")
ちょっと文字コードあたりで苦労したので、そこらへんをちゃんと理解していきたい。
あと便利なものを使うのもいいんだけど、学習っていういみでは元々の仕組みをしることも大事だと思った。