備忘録

備忘録

PythonでRSSを取得する方法

Ⅰ. はじめに

タイトルの通り「PythonRSSを取得する方法」です。

Ⅱ. やり方

1. 必要なモジュールをインストールする
pip install feedparser
2. サンプルプログラムを書く
main.py
#!/usr/bin/env python3
# coding: utf-8
import feedparser
import json

feed = feedparser.parse('https://gigazine.net/news/rss_2.0/', response_headers={"content-type": "text/xml; charset=utf-8"})
for x in feed.entries:
  print(x.title)
  print(x.links[0].href)
#debug
#print(json.dumps(feed, ensure_ascii=False))

実行結果

月に270万円以上の収益を上げる開発スタートアップはどのような成長を遂げてきたのか?
https://gigazine.net/news/20180624-slack-bot-standuply-mmr/

過去の歴史からわかる「技術者の仕事がAIに奪われることがない」理由とは?
https://gigazine.net/news/20180624-history-of-math-teach-future-ai/

GoogleやAppleがあなたのスマホ使用時間を減らそうとしている理由とは?
https://gigazine.net/news/20180624-why-google-apple-want-your-phone-less/
(以下略)