Rubyã®Mechaizeã®ä½¿ãæ¹ãããå¿ããã®ã§ãã¡ã¢ããã
ãµã³ãã«ã«å³æ¸é¤¨ã使ããã¨ãã¦ãããã«ãã«å²¡å´å¸ç«å³æ¸é¤¨ãåå¾ãã¦ã¿ãã
岡å´å¸ç«å³æ¸é¤¨ãFlashã¨ãããå¾ãªããµã¤ãã ã£ããã ã¾ããµã³ãã«ä¾ã¨ãã¦ã
Mechanizeèµ·å
èµ·åãBasicèªè¨¼è¨å®ããã¡ã¤ã«ãåå¾
require 'mechanize' mech = Mechanize.new #BASICèªè¨¼ mech.basic_auth("takuya", "****") ##ãã¼ã¸åå¾ mech.get("http://www.example.com");
ãã¼ã¸åå¾ã移å
mech.get("http://www.yahoo.co.jp"); ##"ãªã¼ã¯ã·ã§ã³"ã¨æ¸ããããªã³ã¯ãã¯ãªãã¯ã mech.page.link_with( :text=>"ãªã¼ã¯ã·ã§ã³").click mech.page.links #ãªã³ã¯ä¸è¦§
ããã§ã¯å®è·µãã¦ã¿ã¾ãããã
Googleã§å²¡å´å¸ç«å³æ¸é¤¨ãæ¤ç´¢ã
Googleã表示ãã¦ãã©ã¼ã ã«å¤ãå ¥ãã¦éä¿¡ããã
mech.get("http://www.google.co.jp"); form = mech.page.forms.first form.field_with( :name => "q").value = "岡å´å¸ç«å³æ¸é¤¨" form.submit mech.page.link_with( :text=>"岡å´å¸ç«å³æ¸é¤¨")
ãã©ã¼ã ã®ãã£ã¼ã«ãã«ã¯ãããããªæ段ã§ã¢ã¯ã»ã¹ã§ããããï¼ï¼
<input type="text" ...> ã <input type="password"> ã¯åå¾ã§ããªã
typeãRubyã§ä½¿ãåèªã ãããEVALã§ã¨ã©ã¼ã«ãªã£ã¦ãNokogiriã®Elementãåããªãã
<input type="text" ...> ã <input type="password"> ã¯åå¾ã§ããªã
å®ä¾ã
# mech.page.forms.first.fields_with :type => "text" (eval):4: warning: Object#type is deprecated; use Object#class (eval):4: warning: Object#type is deprecated; use Object#class (eval):4: warning: Object#type is deprecated; use Object#class (eval):4: warning: Object#type is deprecated; use Object#class => []
ãããã page.searchã§XPATHã§æ»ããã
mech.get("http://mixi.jp/") mech.page.search('//input[@type="password"]')
ãã ããNokogiriã®Elementã«ãªãã®ã§ãã¨ã¯ããµãããã3æã®ä¿®è«ãåç §ãã½ã¼ã¹ã¯å¾ã§æ¢ãã
ãã¼ã¸ã®HTMLãåå¾ããã
mech.get("http://www.google.co.jp"); form = mech.page.forms.first form.field_with( :name => "q").value = "岡å´å¸ç«å³æ¸é¤¨" form.submit mech.page.link_with( :text=>"岡å´å¸ç«å³æ¸é¤¨").click puts mech.page.body.toutf8 # mechanize#page.body ã§åå¾ã§ããã #ä»ã®æ¹æ³ puts mech.page.parser # Nokogiri::Document::to_s ã§åå¾ã§ããã
HTML欲ããã ããªãopen-uri使ãæ¹ãéããã©ãªã
ãªã³ã¯ããã©ã£ã¦èµæ¸æ¤ç´¢ã¾ã§ããã
ãããªãããªã³ã¯ãé£ã¶ã¨æ©æ¢°ã£ã½ãã®ã§äººéãããã¯ãªãã¯ãã¦ãã©ã£ã¦ããã
mech.get("http://www.google.co.jp").forms.first.field_with( :name => "q").value = "岡å´å¸ç«å³æ¸é¤¨". mech.page.forms.first.submit mech.page.link_with( :text=>"岡å´å¸ç«å³æ¸é¤¨").click mech.page.links.first.click mech.page.link_with( :text=>"èµæ¸æ¤ç´¢ã»äºç´").click
èµæ¸æ¤ç´¢ããããã©ã¼ã ãéä¿¡ããã
mech.user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; ; InfoPath.2; .NET CLR 3.5.30729;) " mech.get("http://www.google.co.jp").forms.first.field_with( :name => "q").value = "岡å´å¸ç«å³æ¸é¤¨" mech.page.forms.first.submit mech.page.link_with( :text=>"岡å´å¸ç«å³æ¸é¤¨").click mech.page.links.first.click mech.page.link_with( :text=>"èµæ¸æ¤ç´¢ã»äºç´").click mech.page.forms.first.field_with( :name=>"key1").value="ããã«ã¼ã¨ç»å®¶ " mech.page.forms.first.field_with( :name=>"hidKensakuF").value = 1 mech.page.forms.first.submit
ããç°¡åã§ãã
ãã£ã10è¡ã§æ¸ããããªããã°ã©ã ã§é®æããã¡ãæµããªãã
é§
ã«è¨ç½®ãã¦ããããªã¼ãã¼ãã¼ãå
¨ç¨®é¡æã£ã¦å¸°ã£ã£ã¦é®æããããããªãã®ã ã