LoginSignup
11
11

More than 3 years have passed since last update.

PythonでRSSのスクレイピングする

Last updated at Posted at 2020-06-07

lxml.etreeモジュールを使う

公式

import lxml.etree

#ファイルを読み込む
tree = lxml.etree.parse('rss2.xml')
#getroot()メソッドでXMLのルート要素い対応する
root = tree.getroot()

#xpath()メソッドでXPathにマッチする要素をリストを取得
for item in room.xpath('要素を階層で指定'):
#取得したいデータを指定し、取得

feedparserを使う

RSSのフォーマットを意識せずにスクレイピングできる。RSSフィードには、RSS1.0, RSS2.0,Atomなど複数フォーマットがあって、めんどくさいので、おすすめ。

公式

Dockerのタグがついたqiitaの記事を取得

import feedparser

d = feedparser.parse('https://qiita.com/tags/docker/feed')

for entry in d.entries:
    print(entry.link, entry.title)

とっても簡単なので、、、。定期実行して、自分用のRSSリーダー作れそう。。

参考

Pythonクローリング&スクレイピング

11
11
0

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
11
11