ãã¡ãã£ã¨åã«è©¦ããã¨æã£ã¦ããã®ã¾ã¾æ¾ç½®ãã¦ããruby製ã®ã¯ãã¼ã©ã¼ãMasqueãã試ãã¦ã¿ã¾ãããruby製ã®ã¯ãã¼ã©ã¼ã¯ãä»ã«ã¯Anemoneã¨ããåªç§ãªãã®ãããã¾ãããã®ä¸ã§ä½æ
ã¨ããã¨ãAnemoneã«ã¯ãªãç¹æ§ãããããã§ãã
ãMasqueã¯Capybaraã®DSLã§è¨è¿°åºæ¥ãWebã¯ãã¼ã©ã¼ã§ããã¤ã¾ãCapybaraãåããçºã®ãã®ãªã®ã§ãJavaScriptã解éãåºæ¥ãã¨ãããã¨ã§ããã¾ãå ´åã«ãã£ã¦ã¯ãã¬ã¹ãã³ã·ãã«ãã¶ã¤ã³ã®ãµã¤ãã®ç¢ºèªãåºæ¥ã¾ããä¸æ¹ã§ãAnemoneã¯ããã¾ã§åå¥åå¥ã®HTMLãåå¾ããçºã®ã¯ãã¼ã©ã¼ãªã®ã§ãJavaScriptãå¤ç¨ãã¦ãããµã¤ãã§ã®æ
å ±åå¾ã«åãã¾ãããã©ã¡ããåªãã¦ããã¨ãã訳ã§ã¯ãªãã®ã§ãç¨éã«å¿ãã¦ä½¿ãããªãã°ããã§ãããã
Masqueã®ã¤ã³ã¹ãã¼ã«
$ gem install masque Fetching: headless-1.0.1.gem (100%) Successfully installed headless-1.0.1 Fetching: mini_portile-0.5.2.gem (100%) Successfully installed mini_portile-0.5.2 Fetching: nokogiri-1.6.1.gem (100%) Building native extensions. This could take a while... Successfully installed nokogiri-1.6.1 Fetching: mime-types-2.0.gem (100%) Successfully installed mime-types-2.0 Fetching: rack-1.5.2.gem (100%) Successfully installed rack-1.5.2 Fetching: rack-test-0.6.2.gem (100%) Successfully installed rack-test-0.6.2 Fetching: xpath-2.0.0.gem (100%) Successfully installed xpath-2.0.0 Fetching: capybara-2.1.0.gem (100%) IMPORTANT! Some of the defaults have changed in Capybara 2.1. If you're experiencing failures, please revert to the old behaviour by setting: Capybara.configure do |config| config.match = :one config.exact_options = true config.ignore_hidden_elements = true config.visible_text_only = true end If you're migrating from Capybara 1.x, try: Capybara.configure do |config| config.match = :prefer_exact config.ignore_hidden_elements = false end Details here: http://www.elabs.se/blog/60-introducing-capybara-2-1 Successfully installed capybara-2.1.0 Fetching: capybara-webkit-1.0.0.gem (100%) Building native extensions. This could take a while... ERROR: Error installing masque: ERROR: Failed to build gem native extension. /Users/username/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb Command 'qmake -spec macx-g++' not available Gem files will remain installed in /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/capybara-webkit-1.0.0 for inspection. Results logged to /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/capybara-webkit-1.0.0/./gem_make.out
qmakeããªãã¨ã¨ã©ã¼ãåºã¾ããã¤ã³ã¹ãã¼ã«æ¹æ³ãæ¢ãã¦ã¿ãã¨ãHomebrewã§ã¤ã³ã¹ãã¼ã«åºæ¥ãããã§ããããã±ã¼ã¸åã¯ãqmakeã§ã¯ãªãqtã§ãã
$ brew install qt Warning: Building source; cellar of qt's bottle is /usr/local/Cellar ==> Downloading http://download.qt-project.org/official_releases/qt/4.8/4.8.5/qt ######################################################################## 100.0% ==> ./configure -prefix /Users/username/tools/homebrew/Cellar/qt/4.8.5 -system-zli ==> make ==> make install ==> Caveats We agreed to the Qt opensource license for you. If this is unacceptable you should uninstall. ==> Summary 🍺 /Users/username/tools/homebrew/Cellar/qt/4.8.5: 2778 files, 119M, built in 23.6 minutes
ã³ãã³ãä¸çºã§ã¤ã³ã¹ãã¼ã«åºæ¥ãã®ã§ãããå²ã¨æéãæããã¾ãã
æ°ãåãç´ãã¦ãå度gemã§masqueã®ã¤ã³ã¹ãã¼ã«
$ gem install masque Building native extensions. This could take a while... Successfully installed capybara-webkit-1.0.0 Fetching: http_parser.rb-0.5.3.gem (100%) Building native extensions. This could take a while... Successfully installed http_parser.rb-0.5.3 Fetching: eventmachine-1.0.3.gem (100%) Building native extensions. This could take a while... Successfully installed eventmachine-1.0.3 Fetching: faye-websocket-0.4.7.gem (100%) Building native extensions. This could take a while... Successfully installed faye-websocket-0.4.7 Fetching: poltergeist-1.3.0.gem (100%) Successfully installed poltergeist-1.3.0 Fetching: masque-0.3.1.gem (100%) Successfully installed masque-0.3.1 Parsing documentation for capybara-webkit-1.0.0 Installing ri documentation for capybara-webkit-1.0.0 Parsing documentation for eventmachine-1.0.3 unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/fastfilereaderext.bundle, skipping unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/rubyeventmachine.bundle, skipping Installing ri documentation for eventmachine-1.0.3 Parsing documentation for faye-websocket-0.4.7 unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/faye_websocket_mask.bundle, skipping Installing ri documentation for faye-websocket-0.4.7 Parsing documentation for http_parser.rb-0.5.3 unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/ruby_http_parser.bundle, skipping Installing ri documentation for http_parser.rb-0.5.3 Parsing documentation for masque-0.3.1 Installing ri documentation for masque-0.3.1 Parsing documentation for poltergeist-1.3.0 Installing ri documentation for poltergeist-1.3.0 6 gems installed
ä»åã¯ãããªãã¤ã³ã¹ãã¼ã«åºæ¥ã¾ããæ©éサンプルソースãèµ°ããã¦ã¿ã¾ãããããµã³ãã«ã½ã¼ã¹ã¯ä»¥ä¸ã®éãã§ãã
require "masque" m = Masque.new(:display => 99, :driver => :webkit) # or :driver => :poltergeist m.run do # Capybara::DSL syntax # https://github.com/jnicklas/capybara#the-dsl visit "http://www.google.com/" fill_in("q", :with => "capybara") find('*[name="btnG"]').click titles = evaluate_script <<-JS (function(){ var titles = Array.prototype.map.call( document.querySelectorAll('h3 a'), function(a) { return a.innerText; } ); return titles; })(); JS puts titles.join("\n") end
ãµã³ãã«ã½ã¼ã¹ãå®è¡ãã¦ã¿ãã¨ãä¸è¨ã®ãããªã¨ã©ã¼ãåºã¾ãããXvfbããªãã¨ã®ãã¨ã§ããXvfbã¯ä»®æ³ãã¬ã¼ã ãããã¡ã§ãXãèµ·åãã¦ããªããã·ã³ã§ãXã®ä»®æ³çãªXãã¬ã¼ã ãããã¡ã使ããããã«ããçºã®ãã®ã§ãããããå
¥ãã¦ããã°ãXã®ã©ã¤ãã©ãªã使ããããã«ãªãã¾ãã
$ ruby masque_sample.rb /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/headless-1.0.1/lib/headless/cli_util.rb:9:in `ensure_application_exists!': Xvfb not found on your system (Headless::Exception) from /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/headless-1.0.1/lib/headless.rb:68:in `initialize' from /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/masque-0.3.1/lib/masque.rb:50:in `new' from /Users/username/.rvm/gems/ruby-2.0.0-p247@masque/gems/masque-0.3.1/lib/masque.rb:50:in `initialize' from masque_sample.rb:2:in `new' from masque_sample.rb:2:in `<main>'
ããã¦ã©ããããã®ãã¨èããã®ã§ãããåºæ¬çã«ã¯Xãããã°è¯ãã¯ãã§ããMacã§Xã¯ã©ããªã£ã¦ãããã¨ããã¨ãå
¥ã£ã¦ããªãã®ã§XQuartzã使ãã¨公式ページã«æ¸ãã¦ãã¾ããXQuartzはdmg形式で配布ããã¦ãã®ã§ããã¦ã³ãã¼ããã¦ã¤ã³ã¹ãã¼ã«ãã¾ããããã¤ã³ã¹ãã¼ã«å¾ã«åèµ·åãã¦ç°å¢ãåæ ããã¾ãã
ãå度ãå®è¡ãã¦ã¿ã¾ããä½ããçµæãåºã¦ãã¾ãããããã¯ãmasqueãGoogleã§capybaraãæå®ãã¦æ¤ç´¢ãã¿ã³ãæ¼ãã¦ããã®çµæãåå¾ãã¦ããã®ã§ãã
$ ruby masque_sample.rb jnicklas/capybara · GitHub capybara ã®ç»åæ¤ç´¢çµæ Capybara - Wikipedia, the free encyclopedia ãã¬ã¼ã ã¯ã¼ã¯ã§å®è·µï¼ JavaScriptãã¹ãå ¥éï¼5ï¼ï¼Capybara ... capybara ã§å¿«é©ãªãã¹ãçæ´»ã - SlideShare Capybara Games Capybara - GitHub Pages Capybara ã® README æ訳 - ããããWEBãµã¼ãã¹éçºæ¥è¨ Capybara Madness RSpecã¨Capybaraã§JavaScript/Ajaxããã¹ããã - Rails éæ - Ruby ... capybara | RubyGems.org | your community gem host
Masqueã®æ§é
ãæåã«æ¸ããéããMasqueã¯Capybaraã®DSLãè¨è¿°åºæ¥ãã¯ãã¼ã©ã¼ã§ããããã念é ã«ã½ã¼ã¹ãè¦ãã¨ã以ä¸ã®ã½ã¼ã¹ã®æå³ãç´ã解ãã¾ãã
visit "http://www.google.com/" fill_in("q", :with => "capybara") find('*[name="btnG"]').click
ãã¨ããã¾ã§æ¸ãã¦ãCapybaraã¨ã®éããæ°ã«ãªã£ã¦ãã¾ããåãå¦çãCapybaraã§æ¸ãã¨ãä¸è¨ã®ãããªã½ã¼ã¹ã«ãªãã¾ãã
require 'capybara' require 'capybara-webkit' include Capybara::DSL Capybara.default_driver = :webkit visit "http://www.google.com/" fill_in("q", :with => "capybara") find('*[name="btnG"]').click titles = evaluate_script <<-JS (function(){ var titles = Array.prototype.map.call( document.querySelectorAll('h3 a'), function(a) { return a.innerText; } ); return titles; })(); JS puts titles.join("\n")
ãDSLã®å®£è¨ãªã©ãä¸é¨ã®éããããã ãã§ããçµè«ã¨ãã¦ã¯ãMasqueã¯Capybaraã®ã©ããã¼ã¨ãã¦ä¸é¨ã®å¤ãä¸æ¸ããã¦ä½¿ãããããã¦ããã ãã§ããCapybaraã®DSLã使ãã¾ããã¨ããæç¹ã§ããããããã ã¨ããçµè«ã§ãã
ã¾ã¨ã
ãçµè«çã«ã¯ãCapybaraã®æ§é ãã¡ããã¨ç¥ã£ã¦ããã°ãç¹ã«å¿ è¦ã®ãªãã©ã¤ãã©ãªã§ããä¸æ¹ã§Masqueã使ããã¨ã«ãã£ã¦ãä½è¨ãªå®£è¨ã®æéãå¿ è¦ãªããªãã¾ããã¾ãå¿ è¦ãªã¨ããã¯ä¸æ¸ãã§è¨å®ã§ããããã«ãªã£ã¦ãã¾ããã¾ã使ã£ã¦ãè¯ãããªãã¨ããã¬ãã«ã§ããæ¹åæ§ã¨ãã¦ã¯ãCapybaraã®è¨å®ã便å©ã«ãããããã¯ãã¼ã©ã¼ã¨ãã¦ã®æ©è½ãçãè¾¼ãã æ¹ãé¢ç½ãã¨æãã¾ãããªã³ã¯ã解æãã¦å帰çã«æ å ±ãåéããæ©è½ãªã©ããä»å ããã®ãï¼ã¤ã ã¨æãã¾ãã
See Also:
オープンソースのRubyのWebクローラー"Anemone"を使ってみる
複数並行可能なRubyのクローラー、「cosmicrawler」を試してみた
あらためてRuby製のクローラー、"anemone"を調べてみた
åç
§ï¼
About X11 and OS X
XQuartz
uu59/masque · GitHub
jnicklas/capybara · GitHub
Rubyã«ããã¯ãã¼ã©ã¼éçºææ³
- ä½è : ãã³ãã¡,ä½ã æ¨æé
- åºç社/ã¡ã¼ã«ã¼: SBã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2014/08/25
- ã¡ãã£ã¢: 大åæ¬
- ãã®ååãå«ãããã° (1件) ãè¦ã
Spidering hacksâã¦ã§ãæ å ±ã©ã¯ã©ã¯åå¾ãã¯ããã¯101é¸
- ä½è : Kevin Hemenway,Tara Calishain,æä¸é ç«
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã»ã¸ã£ãã³
- çºå£²æ¥: 2004/05
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 52人 ã¯ãªãã¯: 904å
- ãã®ååãå«ãããã° (103件) ãè¦ã