Skip to content

Instantly share code, notes, and snippets.

@fjustin
Last active December 16, 2016 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjustin/08cd7cbaf79ac7753cb35385de3fe3d5 to your computer and use it in GitHub Desktop.
Save fjustin/08cd7cbaf79ac7753cb35385de3fe3d5 to your computer and use it in GitHub Desktop.
nokogiri
#URLにアクセスするためのライブラリを読み込む
require 'open-uri'
#nokogiriの呼び出し
require 'nokogiri'
#読み込みたいurlを記述する
url='http://www.tcu.ac.jp/topics/index.html'
charset=nil
html = open(url) do |f|
charset=f.charset  #文字種を判別
f.read        #htmlを読み込む
end
#htmlの解析
doc = Nokogiri::HTML.parse(html, nil, charset)
doc.xpath('//div[@class="topics-entry mt10"]').each do|node|
#タイトルの取得
p node.css('h3')
#日付の取得
p node.css('entrydate')
#文言の取得
p node.css('a').attribute('href').value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment