Last active
December 16, 2016 10:32
-
-
Save fjustin/08cd7cbaf79ac7753cb35385de3fe3d5 to your computer and use it in GitHub Desktop.
nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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