æ¨æ¥ã¡ãã£ã¨Phantom.JSã®ç·´ç¿ããï¼
Phantom.JS
PhantomJS: Headless WebKit with JavaScript API
PhantomJS is a headless WebKit with JavaScript API.
ã¤ã³ã¹ãã¼ã«
brew install phantomjs
Qt4ãå¿ è¦ã§ã¤ã³ã¹ãã¼ã«ã«æéããã
å®è¡
phantomjs a.js ã¨ã phantomjs a.coffee ã§å®è¡
CoffeeScriptå®è¡ã§ãã
ãã ãï¼ã³ã³ãã¤ã«ã§ããªãã¨ãã«ã©ããæªããã¨ãã¯æãã¦ãããªã
phantom
ã°ãã¼ãã«ã«phantomã¨ãããªãã¸ã§ã¯ãããã
phantom.exitã¨ã phantom.argsã¨ã
console.log
console.logã§ã³ã³ã½ã¼ã«ã«åºåã§ãã
ãã¼ã¸éã
WebPage#openã§ãã¼ã¸éãã ã³ã¼ã«ããã¯ãæ¸ãã
page = new WebPage() page.open 'http://google.com', (status) -> console.log 'éä¿¡ãã¾ãã' phantom.exit()
ã³ã¼ã«ããã¯ã¯æå®ããURLããã¼ããããã¨ãã«å®è¡ãããã®ã§ã¯ãªãã¦ï¼WebPageã®ãã¼ããå®äºãããæ¯åå¼ã°ãã
ã¤ã³ã¹ã¿ã³ã¹ãã¨ã«è¨å®ããã¿ãããªã¤ã¡ã¼ã¸
ãããã並åã«åãããã¨ãã¯WebPageã®ã¤ã³ã¹ã¿ã³ã¹ãå¥ã«ãã
page1 = new WebPage() page1.open 'http://google.com', (status) -> href = page1.evaluate -> location.href console.log "éä¿¡ãã¾ãã(1) #{href}" page2 = new WebPage() page2.open 'http://bit.ly', (status) -> href = page2.evaluate -> location.href console.log "éä¿¡ãã¾ãã(2) #{href}" page3 = new WebPage() page3.open 'http://hatenablog.com', (status) -> href = page3.evaluate -> location.href console.log "éä¿¡ãã¾ãã(3) #{href}"
æ¨æ¥è¦ã¦ãã¨ï¼phantom.state++ã¨ããã¦ä½åéä¿¡ãããã§å¦çãå¤ããã®ãè¦ã
ãã£ããã²ã©ãæããããï¼ã»ãã«è¯ãæ¸ãæ¹ããããï¼location.hrefãæ£è¦è¡¨ç¾ã«ãããããã¦å¦çãæ¸ãã¨ãã®ã»ããããæ°ããã
phantom.state = 0 page = new WebPage() page.open url, (status) -> try phantom.state++ if status != 'success' phantom.exit(1) switch phantom.state when 1 # 1åç® when 2 # 2åç® catch error phantom.exit(1)
DOMæä½
WebPage#evaluateã§ãã¼ã¸ã®ã³ã³ããã¹ãã§ã¹ã¯ãªãããå®è¡ã§ãã
page.evaluate -> document.querySelector('textarea').value
documentã¨ãåããã®ã§ä½ã§ãã§ãã
ãã ãï¼functionã渡ããã ãã§ï¼ã¯ãã¼ã¸ã£ã¨ãã§å¤ããå¤æ°æ¸¡ãããã§ããªã
ã¨ãããã¨ã¯ï¼èµ·åæã«ã¡ãã»ã¼ã¸ãæ¸ãã¦ãã®å
容ãæ²ç¤ºæ¿ã«ãã¹ãã¨ãã¯ã§ããªãï¼(誰ãç¥ã£ã¦ããæãã¦ã»ãã)
è¿ãå¤ã¯åãã
evaluateã®ä¸ã§console.logãã¦ãä½ãã§ã¦ããªãï¼ãã©ã¦ã¶ã®ä¸ã®console.logãå¼ã°ãã¦ããããï¼
ä¾å¤ãcatchããªãã¨ã¹ã¿ãã¯ãã¬ã¼ã¹ã¨ãåºãã«å¦çãçµäºããå ´åããã
ãããã°
WebPage#render(ãã¡ã¤ã«å)ã§ã¹ã¯ãªã¼ã³ã·ã§ããåãã
ã¹ã¯ãªã¼ã³ã·ã§ãããè¦ã¦ï¼ã¡ããã¨ãã¼ã¸ãåãã¦ãã確èªãã¦ãããã°ããã¨å¹çè¯ãï¼
window.setTimeout -> page.render 'a.png' , 200
çµäº
phantom.exit()ããªãã¨çµäºããªãã®ã§çµããããã¨æã£ãã¨ããã«æ¸ãã¦ãã
phantom.open
æ¤ç´¢ããã¨WebPage#openã§ã¯ãªãã¦phantom.openã使ã£ã¦ãã®ããã£ãï¼
æå
ã®phantomã«ã¯phantom.openãªãï¼
APIãå¤ãã£ãï¼
å°è±¡
ãã©ã¦ã¶ã¨åãããã«åãã®ã¯ä¾¿å©ã ãã©æ¸ãã«ããï¼
JSã§DOMæä½ã§ããã°æ
£ãã¦ãããä»ã®æ
£ãã¦ãã¹ã¯ãªããè¨èªã§æ¸ãããéããã¨æã£ããã©ï¼éä¿¡ããã¨ãããï¼ã³ã¼ã«ããã¯ãå¤ãªæãã ã£ããï¼ãããã°ãã«ããã£ãããã¦ï¼ãªããªãé£ããã£ãï¼
ã¹ã¯ã¬ã¤ãã³ã°ã«ä½¿ãã«ã¯ï¼ãã¼ã¸å´ã®JSãåããªãã¨ã©ããã¦ãæ¸ããªãã¨ããå ´å以å¤ã«ã¯ä½¿ããªãã¦ããããï¼
æ¨æ¥ä½ã£ãã®ã¯ä»¥ä¸ã§ï¼è¨ºæã¡ã¼ã«ã¼ãã©ã³ãã ã«å診ãã¦çµæã表示ããï¼ã¨ãããã®ï¼
ãã¼ã¸ã®JSã®ãã¹ãã¨ãã§ä½¿ãã«ã¯è¯ãæ°ãããï¼