Created
October 30, 2013 11:36
-
-
Save hackugyo/7231212 to your computer and use it in GitHub Desktop.
スクレイピングのテスト http://www.absolute-keitarou.net/blog/?p=634
This file contains hidden or 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
# coding: utf-8 | |
require 'yaml' | |
# ruby2.0.0に組み込み | |
require 'nokogiri' | |
require 'open-uri' | |
require 'term/ansicolor' | |
# gem install term-ansicolor | |
include Term::ANSIColor | |
# yamlで列挙したURLを順番に読み込む | |
urls = YAML.load_file(File.expand_path(File.dirname(__FILE__)+"/urls.yml")) | |
urls.each do |url| | |
html = Nokogiri::HTML(open(url)) | |
# url + タイトル | |
print blue+url+reset+"\n" | |
print yellow+html.search("h3").text+reset+"\n" | |
# メニュー一覧の取得(commod1_l or commod2_lのクラスのオブジェクト取得) | |
menus = html.search(".commod1_l, .commod2_l") | |
menus.each do |menu| | |
# メニュー名、画像URL、カロリー値の表示 | |
puts menu.search("h4").text | |
puts ' ' + menu.search("img").first.attribute("src").value #.inspect | |
puts ' ' + menu.search("p > .calory").text.gsub(/>/, "") #.inspect | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment