趣å³ã§éçºãã¦ããpuppeteer-rubyãã ãã¶ã¾ã¨ãã«åãããã«ãªã£ã¦ããã®ã§ããã®ãããã§ä¸çãã
puppeteer-rubyçèªã®ãã£ãã
ããããã°éå»ã®2è¨äºï¼puppeteer-rubyを作ろうとして挫折した - YusukeIwakiのブログ, やっぱりPuppeteerをRubyから使えないと困るので、puppeteer-rubyを作ることにした - YusukeIwakiのブログï¼ã«ã¯æ¸ãã¦ããªãã£ããpuppeteer-rubyãä½ããã¨æã£ãä¸çªã®çç±ãã
ã¾ããRubyã§èªåãã¹ããããªãSeleniumãããããããªã®ã«Seleniumã§ã¯ãªãpuppeteerã使ãæ大ã®çç±ã¯... waitForSelector
ã ã
ãã¾ã ããªã¼ã¾ã ããªã¼ãã¨ãã¼ãªã³ã°ã§DOMè¦ç´ ãç¾ããã®ãå¾ ã¤Seleniumã¨ã¯å¯¾ç §çã«ããDOMã«å¤åããã£ããé½åº¦ãããã¦ãã¼ãã¨ãªã¹ãã¼ä»æãã¦DOMè¦ç´ ãç¾ããã®ãç£è¦ããã®ãpuppeteerãSeleniumã§ã¯ã¨ãã©ãDOMè¦ç´ ãç¾ããã®ãæ¤ç¥ã§ããªããã¨ãããããpuppeteerã¯ï¼MutationObserverããã°ããªãéãã¯ï¼DOMè¦ç´ ãç¾ããç¬éã絶対ã«éããªãã
ãRubyã§åãå ¥ããã¹ããèªåãã¹ããæ¸ããããã©ãSeleniumã®ç²¾åº¦ã ã¨ããã©ãã ã£ã¦ãã人ã¯ãã¶ãèªå以å¤ã«ãå± ãããããªããã
ãããªæãããpuppeteer-rubyã¯èªçãããï¼æ¯é¢æ» è£ï½ï¼
(2020.08.30 追è¨) Capybaraã§éç¨ä¸ã®åãå ¥ããã¹ããpuppeteer-rubyã§è£å¼·ãããã¨ãã§ãã
ããããªãã¨ãã§ãã¾ããç¨åº¦ã®å 容ã§ãããå¥è¨äºã«è¼ãã¦ã¾ã
ãCapybaraã§æ¸ããåãå ¥ããã¹ãããä½æ ãã¿ã¤ã ã¢ã¦ãã¨ã©ã¼ã§è½ã¡ã¦ããªãã©ã¤ã¨ãè²ã ããã©ãã»ã»ã»ãããã©ãæ¹ãã¦CypressãCodeceptJSãªã©ã§åå®è£ ããã®ãããã©ãã»ã»ã»ãã¨ããå ´åã«ãpuppeteer-rubyã¯Capybaraãç½®ãæãããã®ã§ã¯ãªãå ±åãããã¨ãã§ããã®ã§ãæ¢åãã¹ãã®å¼±ãé¨åã ããpuppeteerã®waitForSelector/waitForNavigationã§è£å¼·ãããã¨ãã§ãã¾ãï¼
æè¿ã®puppeteer-rubyã®ã¢ãããã¼ã
waitForSelectorã¯ã2é±éåãããã«å®è£ ããã
ãã¨ã¯ãå°å³ã«åçã®ã¢ãããã¼ãã¨ãã§ããªãã¨å°ãã®ã§ããã¡ã¤ã«ã®ã¢ãããã¼ããå®è£ ããã
waitForSelectorã§ä¸æ°ã«æ°ãæãã¦ãã¾ã£ãã®ã§ãæè¿ã¯å®ã«ä½èª¿ã§ããã
ã
ãããªãã¨ã¯ã©ãã§ããããèªåæä½ã ã
ç¡é§ãªåç½®ãã¯é·ããããèªåæä½ã®è©±ããããã
puppeteer-rubyãã¤ã³ã¹ãã¼ã«
$ bundle init $ echo 'gem "puppeteer-ruby"' >> Gemfile $ bundle install
ã¨ããããggrksãã¦ã¿ããã
ggrks.rb ãä½ã£ã¦
require 'puppeteer' search_keyword = ARGV.join(" ") search_keyword = "puppeteer" if search_keyword.empty? launch_options = { # ãã©ã¦ã¶ç»é¢ã表示ããªããï¼ãããã¬ã¹ã¢ã¼ããç¡å¹ã«ããï¼ã headless: false, args: [ # ã²ã¹ãã»ãã·ã§ã³ã§æä½ããã "--guest", # ã¦ã¤ã³ãã¦ãµã¤ãºãããã©ã«ããã大ããã«ã '--window-size=1280,800', ], # 人éå³ã®ããé度ã§å ¥å/æä½ããã slow_mo: 50, } Puppeteer.launch(launch_options) do |browser| page = browser.pages.first || browser.new_page page.viewport = Puppeteer::Viewport.new(width: 1280, height: 800) page.goto("https://google.com/") page.type_text("input[name='q']", search_keyword) await_all( page.async_wait_for_navigation, page.keyboard.async_press("Enter"), ) sleep 10 end
以ä¸ã®ããã«å®è¡ããã¨ã»ã»ã»
$ bundle exec ruby ggrks.rb
ããããåãã¦æ¤ç´¢çµæç»é¢ãåºãã
ãµã³ãã«ã¯æ¸ãã¢ããã¼ã·ã§ã³ããªããªã£ã¦ããã®ã§ã»ã»ã»
å®ã¯ãµã³ãã«ã¹ã¯ãªããã®ãªãã¸ããªãç¨æãã¦ããï½
ä»å¾ãå ¬éã§ãããããªï¼ç§å¿æ å ±ãå«ã¾ãªãï¼ã¹ã¯ãªããã¯ã©ãã©ãã¢ãããã¦ãããã¨æãã
ã
ã¾ã¨ã
ãã ã®ãpuppeteer-rubyã®å®£ä¼ã§ããã
ã¾ã ã¡ãã£ã¨ä¸å®å®ã ãã©ããRubyã§åãå ¥ããã¹ããèªåãã¹ããæ¸ããããã©ãSeleniumã®ç²¾åº¦ã ã¨ããã©ããã£ã¦ããã®ã«å ±æããããã¯ãã²ä½¿ã£ã¦ã¿ã¦ãã ããã
(2020.08.16追è¨: 0.0.11ã§ã ãã¶å®å®ãã¦åãããã«ãªãã¾ããã puppeteer-ruby 0.0.11で完走率が劇的に改善。ブラウザの自動操作が快適に。 - YusukeIwakiのブログ )