LoginSignup
0
0

More than 1 year has passed since last update.

nokogiriの使い方メモ

Posted at

初投稿&マークダウン慣れついでに、簡単にnokogiriの使い方のメモ。
nokogiriはRubyでスクレイピングする際に使うライブラリ。

環境

ruby 3.0.0
MacOS Mojave 10.14.4

インストール&バージョン確認

権限エラーがあればsudoつけること

gem install nokogiri
nokogiri -v

タイトル取得

下記のポータルサイトのタイトルをnokogiriを使用して、スクレイピングしてみる。
プログラミングに関するブログ

require 'nokogiri'
require 'open-uri'
 
url = "https://supremecourtlawlibrary.org/".freeze
charset = nil

html = open(url) do |f|
  charset = f.charset
  f.read
end
 
page = Nokogiri::HTML.parse(html, nil, charset)
 
p page.search("title").text

実行結果

"ProCode"

タイトルタグ以外にも、記事のタイトルやURLなど、色々取得できる。
また後日付け加えるので、一旦ここまで。

参考・引用資料
https://supremecourtlawlibrary.org/
https://github.com/sparklemotion/nokogiri
https://nokogiri.org/

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