Rubyのスクレイピングを行っていますが
該当ページの<a href>の中身を取得したいのですが、うまく行きません。
XPATHの指定を変えてもうまく行かないのでご教授願います。
画像の指定している箇所の<href>の中身を取得したいです。
よろしくお願いいたします。
require 'nokogiri' require 'open-uri' require 'csv' urls = %w( https://www.kfm.or.jp/fdb/registration/categorylist/5, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:2, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:3, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:4, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:5, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:6, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:7, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:8, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:9, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:10, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:11, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:12, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:13, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:14, https://www.kfm.or.jp/fdb/registration/categorylist/5/page:11, ) titles = [] charset = nil urls.each do |url| html = open(url) do |f| charset = f.charset #文字種別を取得 f.read #htmlで読み込んで変数htmlに渡す end #htmlをparseしてオブジェクトを作成 doc = Nokogiri::HTML.parse(html, nil, charset) doc.xpath('//h4').each do |node| title = node.css('a').inner_text titles.push(node) end end #これをcsvファイルに書き出す CSV.open('titele.csv', 'w') do |csv| csv << titles end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/26 13:33