Skip to content

Instantly share code, notes, and snippets.

@hackugyo
Created October 30, 2013 11:36
Show Gist options
  • Save hackugyo/7231212 to your computer and use it in GitHub Desktop.
Save hackugyo/7231212 to your computer and use it in GitHub Desktop.
スクレイピングのテスト http://www.absolute-keitarou.net/blog/?p=634
# 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