ºÇ½ª¹¹¿·¡§
kitamomonga 2010ǯ10·î16Æü(ÅÚ) 16:33:09ÍúÎò
¡Ö¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ HTML¡×¤òɽ¤¹¥¯¥é¥¹¤Ç¤¹¡£¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿¥Õ¥¡¥¤¥ë¤¬ HTML¡¢XHTML¡¢WAP ¤À¤Ã¤¿¤È¤¤ËÊÖ¤êÃͤˤ³¤Î¥¯¥é¥¹¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬»ÈÍѤµ¤ì¤Þ¤¹¡£
HTML ¹½Â¤¤ò²òÀϤ·¤¿·ë²Ì¤òÆâÉô¤ÇÊÝ»ý¤·¤Æ¤¤¤Þ¤¹¡£
¥Õ¥©¡¼¥à¤ò¾ò·ï¤Çõ¤·¤ÆÁàºî¤·¤¿¤¤¤È¤¤Ï #form_with ¥á¥½¥Ã¥É¤ò¡¢
# <form name="f1"> ¤Ê¥Õ¥©¡¼¥à¤òõ¤·¤Æ¥Ö¥í¥Ã¥¯¤ËÅϤ¹ agent.page.form_with(;name => 'f1'){|form| form....¥ê¥ó¥¯¤ò¾ò·ï¤Çõ¤·¤ÆÁàºî¤·¤¿¤¤¤È¤¤Ï #links_with ¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤Þ¤¹¡£
# <a href="./top.html>Ìá¤ë</a> ¤Ê¥ê¥ó¥¯¤òõ¤·¤Æ¥Ö¥í¥Ã¥¯¤ËÅϤ¹ agent.page.link_with(;:text => 'Ìá¤ë'.toutf8){|link| link....
ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤È¥Ñ¡¼¥¹¤Î¼ºÇԤˤĤ¤¤Æ
Mechanize 1.0.0 ¤Îɸ½à¤Ç Page ¥¯¥é¥¹¤¬»ÈÍѤµ¤ì¤ë HTTP ¥Ø¥Ã¥À¤Î Content-Type ¤ÎÃͤϰʲ¼¤ÎÄ̤ê¤Ç¤¹¡£HTML ¤È XHTML ¤È XHTML MP ¤Ë¤Ê¤ê¤Þ¤¹¡£
- text/html
- application/xhtml+xml
- application/vnd.wap.xhtml+xml
Mechanize::File ¤ò·Ñ¾µ¤·¤Æ¤¤¤Þ¤¹¡£
- new - Mechanize::Page ¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®
- at - ¾ò·ï¤Ë³ºÅö¤¹¤ë HTML ¥Î¡¼¥É¤ò¤Ò¤È¤ÄÊÖ¤¹
- body - ¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ HTML ¤òÊÖ¤¹
- code - ¥Õ¥¡¥¤¥ë¼èÆÀ»þ¤Î HTTP ¥ê¥¶¥ë¥È¥³¡¼¥É¿ô»úʸ»úÎó¤òÊÖ¤¹
- content - alias #body
- content_type - Content-Type ¥Ø¥Ã¥À¤ÎÃͤòÊÖ¤¹
- encoding - ¥Ñ¡¼¥¹¤Ë»ÈÍѤ·¤¿Ê¸»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤òÊÖ¤¹
- encoding= - HTML ¤Îʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÃͤò»ØÄê
- form - alias #form_with
- form_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤ÎForm¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ú¡¼¥¸¤«¤é¤Ò¤È¤ÄÈ´¤½Ð¤·¤ÆÊÖ¤¹
- forms - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë form Í×ÁǤΥե©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- forms_with - ¾ò·ï¤òËþ¤¿¤¹Form¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Õ¥©¡¼¥à¤«¤éÁ´¤ÆÈ´¤½Ð¤·¤ÆÇÛÎó¤ÇÊÖ¤¹
- frame - alias #frame_with
- frame_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤Îframe¤òɽ¤¹Frame¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ú¡¼¥¸¤«¤é¤Ò¤È¤ÄÈ´¤½Ð¤·¤ÆÊÖ¤¹
- frames - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë frame Í×ÁǤΥª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- frames_with - ¾ò·ï¤òËþ¤¿¤¹frame¤òɽ¤¹Frame¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Õ¥©¡¼¥à¤«¤éÁ´¤ÆÈ´¤½Ð¤·¤ÆÇÛÎó¤ÇÊÖ¤¹
- header - alias #response
- iframe - alias #iframe_with
- iframe_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤Îiframe¤òɽ¤¹Frame¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ú¡¼¥¸¤«¤é¤Ò¤È¤ÄÈ´¤½Ð¤·¤ÆÊÖ¤¹
- iframes - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë iframe Í×ÁǤΥª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- iframes_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤Îiframe¤òɽ¤¹Frame¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Õ¥©¡¼¥à¤«¤éÁ´¤ÆÈ´¤½Ð¤·¤ÆÇÛÎó¤ÇÊÖ¤¹
- link - alias #link_with
- link_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤ÎLink¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ú¡¼¥¸¤«¤é¤Ò¤È¤ÄÈ´¤½Ð¤·¤ÆÊÖ¤¹
- links - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë a Í×ÁÇ¤È area Í×ÁǤΥê¥ó¥¯¥ª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- links_with - ¾ò·ï¤òËþ¤¿¤¹Link¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Õ¥©¡¼¥à¤«¤éÁ´¤ÆÈ´¤½Ð¤·¤ÆÇÛÎó¤ÇÊÖ¤¹
- mech - ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ¤·¤¿¤È¤¤Î Mechanize ¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤¹
- parser - HTML ¤ò HTML ¥Ñ¡¼¥µ¤Ç²òÀϤ·¤¿·ë²Ì¤Î¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤¹
- response - ¥µ¡¼¥Ð¤«¤é¤Î¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤òÊÖ¤¹
- root - alias #parser
- save_as - HTML ¤ò¥Ç¥£¥¹¥¯¤ËÊݸ¤¹¤ë
- save - alias #save_as
- search - ¾ò·ï¤Ë³ºÅö¤¹¤ë HTML ¥Î¡¼¥É¤òÁ´¤ÆÊÖ¤¹
- title - ¥Ú¡¼¥¸¤Î¥¿¥¤¥È¥ë¤òÊÖ¤¹
- uri - ¥µ¡¼¥Ð¤Ë¥¢¥¯¥»¥¹¤·¤¿ºÝ¤Î URI ¤òÊÖ¤¹
- / - alias #search
- bases - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë base Í×ÁǤΥª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- base_with - ¾ò·ï¤òËþ¤¿¤¹ºÇ½é¤ÎBase¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ú¡¼¥¸¤«¤é¤Ò¤È¤ÄÈ´¤½Ð¤·¤ÆÊÖ¤¹
- bases_with - ¾ò·ï¤òËþ¤¿¤¹Base¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Õ¥©¡¼¥à¤«¤éÁ´¤ÆÈ´¤½Ð¤·¤ÆÇÛÎó¤ÇÊÖ¤¹
- body= - ¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ HTML ¤ò»ö¸å¤ÇÃÖ¤´¹¤¨¤ë
- code= - ¥Õ¥¡¥¤¥ë¼èÆÀ»þ¤Î HTTP ¥ê¥¶¥ë¥È¥³¡¼¥É¿ô»úʸ»úÎó¤ò»ö¸å¤Ç½ñ¤´¹¤¨¤ë
- filename - Êݸ¥á¥½¥Ã¥É¤Ç¤Î¥Ç¥Õ¥©¥ë¥È¤Ç»ÈÍѤµ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ñ¥¹¤òÊÖ¤¹
- filename= - Êݸ¥á¥½¥Ã¥É¤Ç¤Î¥Ç¥Õ¥©¥ë¥È¤Ç»ÈÍѤµ¤ì¤ë¥Õ¥¡¥¤¥ë¥Ñ¥¹¤ò»ØÄꤹ¤ë
- images - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë img Í×ÁǤΥª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- image_urls - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë²èÁü¤Î URL ¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- labels - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë label Í×ÁǤΥª¥Ö¥¸¥§¥¯¥È¤òÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- labels_hash - ¥é¥Ù¥ëʸ»úÎó¤ÈLabel¥ª¥Ö¥¸¥§¥¯¥È¤ÎÂбþ¤ò³ÊǼ¤·¤¿Hash¤òÊÖ¤¹
- mech= - ¤³¤Î¥Ú¡¼¥¸¤«¤é¥¢¥¯¥»¥¹¤ò¹Ô¤¦ºÝ¤Î Mechanize ¥ª¥Ö¥¸¥§¥¯¥È¤ò»ØÄê
- meta - ¥Ú¡¼¥¸¤Ë¸ºß¤¹¤ë meta Í×ÁǤǥê¥Õ¥ì¥Ã¥·¥å¤Ë´Ø¤ï¤ë¤â¤Î¤ò¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÁ´¤ÆÇÛÎó¤ÇÊÖ¤¹
- response= - ¥µ¡¼¥Ð¤«¤é¤Î¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤ò»ö¸å¤Ç½ñ¤´¹¤¨¤ë
- uri= - ¥µ¡¼¥Ð¤Ë¥¢¥¯¥»¥¹¤·¤¿ºÝ¤Î URI ¤ò»ö¸å¤Ç½ñ¤´¹¤¨¤ë
Mechanize::Page.new(uri=nil, response=nil, body=nil, code=nil, mech=nil){|page| optional_non_ensure_block} ![](https://static.seesaawiki.jp/img/usr_second/common/icon_pen.gif)
¥Ú¡¼¥¸¤òɽ¤¹ Mechanize::Page ¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤·¤Þ¤¹¡£¥æ¡¼¥¶¡¼¤¬»ÈÍѤ¹¤ë¤³¤È¤ÏÄ̾濫¤ê¤Þ¤»¤ó¡£
4¤Ä¤Î°ú¿ô¤Ë´Ø¤·¤Æ¤Ï·Ñ¾µ¤â¤È¤Î Mechanize::File.new ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î Page.new ¤Î response °ú¿ô¤Ë¤Ï¡¢HTML ¤Ç¤¢¤ë¤³¤È¤ò³ÎÄꤹ¤ë¤¿¤á¤Ë {'content-type' => 'text/html'} ¤¬É¬¿Ü¤Ç¤¹¡£¥Ø¥Ã¥À̾¤Ï¾®Ê¸»ú¤ò¿ä¾©¤·¤Þ¤¹¡£¡Ö¶õ¤Î¥Ú¡¼¥¸¡×Åù¤ÎºîÀ®¤Ï¡¢¥Ø¥Ã¥À¤¬¤Ê¤¯¤Æ¤âÆ°ºî¤¹¤ë·Ñ¾µ¤â¤È¤Î Mechanize::File.new ¤¬´ÊÊؤʤΤǤª´«¤á¤Ç¤¹¡£
File ¤Ë̵¤«¤Ã¤¿ Âè 5°ú¿ô¤Î mech ¤Ï¤³¤Î¥Ú¡¼¥¸¤Î¥ê¥ó¥¯¤Î¥¯¥ê¥Ã¥¯¤ä¥Õ¥©¡¼¥àÁ÷¿®¤Ë»ÈÍѤ¹¤ë Mechanize ¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¹¡£¤³¤ì¤ò»ØÄꤷ¤Ê¤¤¤È¤³¤Î¥¯¥é¥¹¤Î¥á¥½¥Ã¥É¤«¤é¥µ¡¼¥Ð¥¢¥¯¥»¥¹¤¬¤Ç¤¤Ê¤¤¤Î¤Ç¼Â¼Áɬ¿Ü¤Ç¤¹¡£
¤è¤¯»È¤ï¤ì¤ëµ½Ò¤Ï°Ê²¼¤Î¤è¤¦¤Ê¤â¤Î¤Ç¤¹¡£
# example.com.html ¤ÎÆâÍƤò http://example.com/ ¤Î¥Ú¡¼¥¸¤È¤ß¤Ê¤¹ page = Mechanize::Page.new( URI.parse('http://example.com/'), {'content-type' => 'text/html'}, File.open('example.com.html', 'rb'){|f| f.read}, '200', agent) # ¼«ºî¤Î¥Ú¡¼¥¸¤òÍúÎò¤ËÅÐÏ¿¤¹¤ë agent.__send__(:add_to_history, page) agent.page.uri #=> http://example.com/Âè 2°ú¿ô response ¤Î content-type ¤ÎÃÍ¤Ë text/html ¤« application/xhtml+xml ¤« application/vnd.wap.xhtml+xml ¤¬´Þ¤Þ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢Mechanize::ContentTypeError? ¤¬È¯À¸¤·¤Þ¤¹¡£
°ú¿ô¤Î¾ò·ï¤Ë³ºÅö¤¹¤ë HTML ¥Î¡¼¥É¤ò #parser ¤«¤éõ¤·¡¢¹çÃפ¹¤ëºÇ½é¤Î¤â¤Î¤ò¤Ò¤È¤ÄÊÖ¤·¤Þ¤¹¡£
parser.at(xpath_or_css) ¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£Mechanize#html_parser ¤¬¥Ñ¡¼¥¹¤ÇÊÖ¤¹¥ª¥Ö¥¸¥§¥¯¥È¤Î at ¥á¥½¥Ã¥É¤Î²òÀâ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¥Ç¥Õ¥©¥ë¥È¥Ñ¡¼¥µ¤Î Nokogiri ¤Î¾ì¹ç¡¢XPath ¤« CSS3 µË¡¤Ç HTML ¤Î¥Î¡¼¥É¸¡º÷¤¬¤Ç¤¤Þ¤¹¡£
¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ HTML¡Ê¤äXHTML¤Ê¤É¡Ë¤òÊÖ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#body ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¼«ÂΤÏÊѲ½¤·¤Æ¤¤¤Þ¤»¤ó¤¬¡¢Ruby1.9 ¤Î¾ì¹ç¡¢ASCII-8BIT ¤Ë force_encoding ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£ÆâÉô½èÍý¾å¤Ï¤³¤ì¤Ç¤âÌäÂê¤Ê¤¤¤Î¤Ç¤¹¤¬¡¢³°Éô¤«¤é Page#body ¤È¤·¤ÆÍøÍѤ¹¤ë¤è¤¦¤Ê¾ì¹ç¤Ïµ¤¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î¥Õ¥¡¥¤¥ë¤ò¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ºÝ¤Î¥ê¥¶¥ë¥È¥³¡¼¥É¤ò "200" ¤Ê¤É¤Î 3·å¤Î¿ô»ú¤Îʸ»úÎó¤ÇÊÖ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#code ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#response ¤«¤é "content-type" ¤È¤¤¤¦¾®Ê¸»ú¥¡¼¤òõ¤·¡¢¤½¤ÎÃͤÎʸ»úÎó¤òÊÖ¤·¤Þ¤¹¡£Â¸ºß¤·¤Ê¤¤¾ì¹ç¤Ï nil ¤òÊÖ¤·¤Þ¤¹¡£
Ä̾¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤Î Content-Type ¥Ø¥Ã¥À¤ÎÃͤ¬ÊÖ¤ê¤Þ¤¹¡£charset ¥È¡¼¥¯¥ó¤Ê¤É¤â¤½¤Î¤Þ¤ÞÊÖ¤µ¤ì¤Þ¤¹¡£
Content-Type: text/html; charset=utf-8¤È¤¤¤¦¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤À¤Ã¤¿¾ì¹ç¡¢¤³¤Î content_type ¥á¥½¥Ã¥É¤Ï "text/html" ¤Ç¤Ï¤Ê¤¯ "text/html; charset=utf-8" ¤È¤¤¤¦Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
#parser ¤Î¥Ñ¡¼¥¹»þ¤Ë»ÈÍѤ·¤¿¡Ê¤Èparser¤¬¼çÄ¥¤¹¤ë¡Ëʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°Ì¾¤òʸ»úÎó¤ÇÊÖ¤·¤Þ¤¹¡£
Mechanize#html_parser ¤¬ Nokogiri ¤Ç¤Ï¤Ê¤¤¾ì¹ç¡¢nil ¤¬ÊÖ¤ê¤Þ¤¹¡£
HTML ¤Ë meta charset
<meta http-equiv="content-type" content="text/html; charset=UTF-8">¤¬Â¸ºß¤·¤¿¾ì¹ç¤Ï¤ª¤ª¤à¤Í¤½¤ì¤ÈƱ¤¸¤â¤Î¤¬ÊÖ¤ê¤Þ¤¹¤¬¡ÊÎã¤Î¾ì¹ç"UTF-8"¡Ë¡¢¥È¥é¥Ö¥ë»þ¤Ë¤Ï¤³¤Î meta charset ¤òÆɤà¹Ô°Ù¤´¤È¥ß¥¹¤Ã¤Æ¤ë¤³¤È¤¬Â¿¤¤¤Î¤Ç¡¢¥Ñ¡¼¥¹¤Î¥È¥é¥Ö¥ë»þ¤Î¸¶°øµæÌÀ¤Ë¤Ï¤¢¤Þ¤ê¸þ¤¤Þ¤»¤ó¡£¼ÂºÝ¤Î HTML ¤ÎÃͤ¬É¬Íפʾì¹ç¤Ï¿ÍÎϤÇľÀÜ HTML ¤ËÅö¤¿¤Ã¤Æ¤¯¤À¤µ¤¤¡£ºÆÀßÄê¤Ï #encoding= ¤Ç¹Ô¤¤¤Þ¤¹¡£
HTML ¤ò¥Ñ¡¼¥¹¤¹¤ë¤Î¤ËÂÅÅö¤Êʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÃͤòÌÀ¼¨Åª¤Ëʸ»úÎó¤ÇºÆÀßÄꤷ¡¢#parser ¤ò¹¹¿·¤·¤Þ¤¹¡£
agent.get(x_sjis_html) p agent.page.encoding #=> "x-sjis" agent.page.encoding = 'CP932' p agent.page.encoding #=> "CP932"°ú¿ô encoding_name ¤Ï iconv ¤Ë¤½¤Î¤Þ¤ÞÅϤµ¤ì¤ë¤Î¤Ç¡¢Mechanize ¤ÎÆ°ºî¤·¤Æ¤¤¤ë¥Þ¥·¥ó¤Ë¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤Æ¤¤¤ë iconv ¤¬²ò¼á²Äǽ¤ÇÆ°ºî²Äǽ¤Ê¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°Ì¾¤ò¤¦¤Þ¤¯»ØÄꤷ¤Æ¤¯¤À¤µ¤¤¡£CP932 ¤ä EUC-JP¡¢ISO-2202-JP¡¢UTF-8¡¢Windows-1252 ¤Ê¤É¤Ï¤ª¤½¤é¤¯¤É¤ó¤Ê´Ä¶¤Ç¤â͸ú¤Ç¤·¤ç¤¦¡Êiconv¤Ç¤ÎShift_JIS¤Ï¡¢ßÁ¤Ê¤É¤Î³Èĥʸ»ú¤ò´Þ¤ó¤Ç¤¤¤Ê¤¤¤Î¤Ç¥¨¥é¡¼¤Ë¤Ê¤ê¤Þ¤¹¡£CP932¤ò»ØÄꤷ¤Æ¤¯¤À¤µ¤¤¡Ë¡£
ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°»ØÄê¸å¡¢¤½¤Î¿·¤·¤¤Ê¸»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ò»ÈÍѤ·¤Æ #parser ¤¬¹¹¿·¤µ¤ì¤Þ¤¹¡£¤¿¤À¤·¡¢°ú¿ô encoding_name ¤¬ #encoding ¤ÎÊÖ¤êÃͤÎʸ»úÎó¤òÂçʸ»ú¾®Ê¸»ú̵»ë¤Ç´Þ¤ó¤Ç¤¤¤ë¾ì¹ç¡Ê/#{encoding}/i =~ encoding_name¡Ë¡¢ºÆ¥Ñ¡¼¥¹¤Ï¹Ô¤ï¤ì¤Þ¤»¤ó¡£
¤É¤ó¤Ê¾ì¹ç¤Ç¤âɬ¤º encoding_name ¤È¤ß¤Ê¤·¤ÆºÆ¥Ñ¡¼¥¹¤ò¹Ô¤Ã¤ÆÍߤ·¤¤¤È¤¤¤¦¾ì¹ç¤Ï¡¢»öÁ°¤Ë¼êÆ°¤Ç¥¤¥ó¥¹¥¿¥ó¥¹ÊÑ¿ô @parser ¤ò¾Ãµî¤·¤Þ¤¹¡£
agent.page.instance_variable_set(:@parser, nil) agent.page.encoding = "IconvŪ¤ËÀµ¤·¤¤HTML¤Îʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°Ì¾"¤Þ¤¿¡¢ºÆ¥Ñ¡¼¥¹¤·¤Æ¤â´û¸¤Î¥Õ¥©¡¼¥à¤ä¥ê¥ó¥¯¤òÊÝ»ý¤·¤¿¥¤¥ó¥¹¥¿¥ó¥¹ÊÑ¿ô¤Ï¥ê¥»¥Ã¥È¤µ¤ì¤Þ¤»¤ó¡£#forms ¤ä #links ¤Ê¤É¤¬¥Ú¡¼¥¸¤ÎÊÖ¤êÃÍɽ¼¨»þ¤Ë¸Æ¤Ð¤ì¤Æ¤·¤Þ¤¦ irb ¤Ç»þ¡¹ÌäÂê¤Ë¤Ê¤ê¤Þ¤¹¡£¤Ê¤Ë¤«ÌäÂ꤬µ¯¤¤¿¾ì¹ç¤Ï³ºÅö¤¹¤ë¥¤¥ó¥¹¥¿¥ó¥¹ÊÑ¿ô¤ò instance_variable_set ¤Ç nil ¤Ë¤¹¤ë¤«¡¢³ºÅö¥¤¥ó¥¹¥¿¥ó¥¹ÊÑ¿ô¤ÎÀ¸À®¼«ÂΤò¤Ê¤ó¤È¤«¤·¤ÆÍÞÀ©¤·¤Æ¤¯¤À¤µ¤¤¡£
irb> agent.get(bad_uri) ¡Êforms¤älinks¤Î·ë²Ì¤¬inpsect¤Ç¼«Æ°É½¼¨¤µ¤ì¤ë¡Ë irb> agent.page.encoding = 'correct encoding' irb> agent.page.links... # @links ¤¬ºÆ¥Ñ¡¼¥¹Á°¤Ë¥»¥Ã¥È¤µ¤ì¤Æ¤¤¤ë¤Î¤ÇÀΤÎÊÑ¤Ê¤Þ¤Þ irb> agent.history.clear irb> agent.get(bad_uri); nil irb> agent.page.encoding = 'correct encoding' irb> agent.page.links...
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Form ¥ª¥Ö¥¸¥§¥¯¥È¤ò #forms ¤«¤éõ¤·¡¢ºÇ½é¤Î¤â¤Î¤òÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxx_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
͸ú¤Ê¥á¥½¥Ã¥É¤ÈÃͤΥڥ¢¤Ï Mechanize::Form ¤Î¥á¥½¥Ã¥É¤ÈÊÖ¤êÃͤˤʤê¤Þ¤¹¡£
# name ¤¬ f1 ¤Ê¥Õ¥©¡¼¥à¤òõ¤·¤Æ¥Ö¥í¥Ã¥¯¤ËÅϤ¹ agent.page.form_with(:name => 'f1'){|form| form.field_with... }
# action °À¤¬ /search ¤Ê¥Õ¥©¡¼¥à¤òõ¤¹ form = agent.page.form_with(:action => '/search')
#parser ¤«¤é form Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Form ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
form Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @forms ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¾ò·ï¤Ë¤è¤ëÃê½Ð¤Ï #form_with ¤È #forms_with ¤Ç¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Form ¥ª¥Ö¥¸¥§¥¯¥È¤ò #forms ¤«¤éõ¤·¡¢Á´¤Æ¤òÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxxs_with ¤ò¡¢¾ò·ï¤Ë¤Ä¤¤¤Æ¤Ï #form_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤ò #frames ¤«¤éõ¤·¡¢ºÇ½é¤Î¤â¤Î¤òÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxx_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
͸ú¤Ê¥á¥½¥Ã¥É¤ÈÃͤΥڥ¢¤Ï Mechanize::Page::Frame ¤Î¥á¥½¥Ã¥É¤ÈÊÖ¤êÃͤˤʤê¤Þ¤¹¡£
# src ¤¬ left.html ¤Ç»Ø¤·¼¨¤µ¤ì¤ë¥Õ¥ì¡¼¥à¤Î HTML ¤ò¼èÆÀ agent.page.frame_with(:src => 'left.html').click # name ¤¬ left ¤Ç¤¢¤ë¥Õ¥ì¡¼¥à¤Î HTML ¤ò¼èÆÀ agent.page.frame_with(:name => 'left').click
#parser ¤«¤é frame Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
frame Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @frames ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¾ò·ï¤Ë¤è¤ëÃê½Ð¤Ï #frame_with ¤È #frames_with ¤Ç¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤ò #frames ¤«¤éõ¤·¡¢Á´¤Æ¤òÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxxs_with ¤ò¡¢¾ò·ï¤Ë¤Ä¤¤¤Æ¤Ï #frame_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤ò #iframes ¤«¤éõ¤·¡¢ºÇ½é¤Î¤â¤Î¤òÊÖ¤·¤Þ¤¹¡ÊIFrame¥¯¥é¥¹¤È¤¤¤¦¤â¤Î¤Ï¤¢¤ê¤Þ¤»¤ó¡Ë¡£
Æ°ºî¤Ï Mechanize::Form#xxxx_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
͸ú¤Ê¥á¥½¥Ã¥É¤ÈÃͤΥڥ¢¤Ï Mechanize::Page::Frame ¤Î¥á¥½¥Ã¥É¤ÈÊÖ¤êÃͤˤʤê¤Þ¤¹¡£
# src ¤¬ inner.html ¤Ç»Ø¤·¼¨¤µ¤ì¤ë¥¤¥ó¥é¥¤¥ó¥Õ¥ì¡¼¥à¤Î HTML ¤ò¼èÆÀ agent.page.iframe_with(:src => 'inner.html').click
#parser ¤«¤é iframe Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡ÊIFrame¥¯¥é¥¹¤È¤¤¤¦¤â¤Î¤Ï¤¢¤ê¤Þ¤»¤ó¡Ë¡£
iframe Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @iframes ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¾ò·ï¤Ë¤è¤ëÃê½Ð¤Ï #iframe_with ¤È #iframes_with ¤Ç¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Frame ¥ª¥Ö¥¸¥§¥¯¥È¤ò #iframes ¤«¤éõ¤·¡¢Á´¤Æ¤òÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡ÊIFrame¥¯¥é¥¹¤È¤¤¤¦¤â¤Î¤Ï¤¢¤ê¤Þ¤»¤ó¡Ë¡£
Æ°ºî¤Ï Mechanize::Form#xxxxs_with ¤ò¡¢¾ò·ï¤Ë¤Ä¤¤¤Æ¤Ï #iframe_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Link ¥ª¥Ö¥¸¥§¥¯¥È¤ò #links ¤«¤éõ¤·¡¢ºÇ½é¤Î¤â¤Î¤òÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxx_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
͸ú¤Ê¥á¥½¥Ã¥É¤ÈÃͤΥڥ¢¤Ï Mechanize::Page::Link ¤Î¥á¥½¥Ã¥É¤ÈÊÖ¤êÃͤˤʤê¤Þ¤¹¡£
¤Ê¤ª¡¢Mechanize::Page::Link ¤Ë name ¥á¥½¥Ã¥É¤¬¤Ê¤¤¤Î¤Ç¡¢Â¾¤Î xxxx_with ¤Î¤è¤¦¤Ë°ú¿ô¤Ëʸ»úÎó¤òÅϤ¹¤È¥¨¥é¡¼¤Ë¤Ê¤ê¤Þ¤¹¡£
# href ¤¬ index.html ¤Ê¥ê¥ó¥¯¤òõ¤·¤Æ¥¯¥ê¥Ã¥¯ agent.page.link_with(:href => 'index.html'){|link| link.click } # text ¤ÎÊÖ¤êÃͤ¬¡Ö¥Û¡¼¥à¡×¤Ë¥Þ¥Ã¥Á¤¹¤ë¥ê¥ó¥¯¤òõ¤·¤Æ¥¯¥ê¥Ã¥¯ agent.page.link_with(:text => /¥Û¡¼¥à/).click # utf-8
#parser ¤«¤é a Í×ÁÇ¤È area Í×ÁǤò¸¡º÷¤·¡¢Mechanize::Page::Link ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
a Í×ÁǤâ area Í×ÁǤâ̵¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
½Ð¸½½ç¤Î a Í×ÁǤΠLink ¥ª¥Ö¥¸¥§¥¯¥È¤Î¤¢¤È¡¢½Ð¸½½ç¤Î area Í×ÁǤΠLink ¥ª¥Ö¥¸¥§¥¯¥È¤¬ÇÛÎó¤ËÆþ¤Ã¤Æ¤¤¤Þ¤¹¡£²¿¤é¤«¤ÎÍýͳ¤Çº®¹ç¤Î½Ð¸½½çÇÛÎó¤¬Íߤ·¤¤¾ì¹ç¤Ï links.sort_by{|link| link.node} ¤·¤Æ¤¯¤À¤µ¤¤¡£
ÆâÉôÊÑ¿ô @links ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¾ò·ï¤Ë¤è¤ëÃê½Ð¤Ï #link_with ¤È #links_with ¤Ç¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Link ¥ª¥Ö¥¸¥§¥¯¥È¤ò #links ¤«¤éõ¤·¡¢Á´¤Æ¤òÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxxs_with ¤ò¡¢¾ò·ï¤Ë¤Ä¤¤¤Æ¤Ï #link_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î¥Ú¡¼¥¸¤¬´Þ¤à¥ê¥ó¥¯¥ª¥Ö¥¸¥§¥¯¥È¤ä¥Õ¥©¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤«¤é Mechanize::Page::Link#click Åù¤Ç¥¢¥¯¥»¥¹¤ò¹Ô¤¦ºÝ¤Ë»ÈÍѤ¹¤ë Mechanize ¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤·¤Þ¤¹¡£
¥Ç¥Õ¥©¥ë¥È¤Ï¤³¤Î Page ¥ª¥Ö¥¸¥§¥¯¥È¤¬ Mechanize::Page.new ¤ÇºîÀ®¤µ¤ì¤¿¤È¤¤Ë°ú¿ô¤ÇÀßÄꤵ¤ì¤¿¤â¤Î¤Ç¤¹¡£ÀßÄꤵ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï nil ¤Ç¤¹¡£
nil ¤Ç¤¢¤ë¾ì¹ç¡¢Mechanize::Page::Link#click ¥á¥½¥Ã¥É¤ä Mechanize::Form#click_button ¥á¥½¥Ã¥É¤Ë¤è¤ë¥ê¥ó¥¯¥¢¥¯¥»¥¹¤ä¥Õ¥©¡¼¥à¥¢¥¯¥»¥¹¤¬¤Ç¤¤Ê¤¯¤Ê¤ê¤Þ¤¹¡Êagent.click(agent.page.links[0]) Åù¤ÏÆ°ºî¤·¤Þ¤¹¡Ë¡£
ºÆÀßÄê¤Ï #mech= ¤Ç¹Ô¤¤¤Þ¤¹¡£
#body ¤ò HTML ¥Ñ¡¼¥µ Mechanize#html_parser ¤Ç¥Ñ¡¼¥¹¤·¤¿·ë²Ì¤Î²òÀϺѤߥª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤·¤Þ¤¹¡£
¥Ç¥Õ¥©¥ë¥È¥Ñ¡¼¥µ¤Î Nokogiri ¤Ç¤Ï Nokogiri::HTML::Document ¤« Nokogiri::XML::Document ¥¯¥é¥¹¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬¡¢¥ª¥×¥·¥ç¥Ê¥ë¤Ê Hpricot ¤Ç¤Ï Hpricot::Doc ¥¯¥é¥¹¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬ÊÖ¤ê¤Þ¤¹¡£
#encoding= ¤Ë¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°Ì¾¤Î°ú¿ô¤¬ÅϤµ¤ì¤¿¤È¤¡¢ºÆ¥Ñ¡¼¥¹¤¬¹Ô¤ï¤ì¤Þ¤¹¡£¥Ñ¡¼¥¹ÆâÍƤ¬´Ö°ã¤Ã¤Æ¤¤¤ë¤È¤¤Ï #encoding= ¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£
Nokogiri ¤ò»ÈÍѤ·¤Æ¤¤¤ë¾ì¹ç¡¢¥Ú¡¼¥¸¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤¿¤È¤¤Ï¤³¤ÎÊÖ¤êÃͤΠerrors ¥á¥½¥Ã¥É¤Ë³Æ¼ï¥¨¥é¡¼¥ª¥Ö¥¸¥§¥¯¥È¤¬³ÊǼ¤µ¤ì¤Þ¤¹¡£
puts agent.page.parser.errors·ë²Ì¡§
input conversion failed due to input error, bytes 0x87 0x40 0x87 0x41 input conversion failed due to input error, bytes 0x87 0x40 0x87 0x41 htmlCheckEncoding: encoder error input conversion failed due to input error, bytes 0x87 0x40 0x87 0x41 encoder error
¥µ¡¼¥Ð¤«¤éÁ÷¤é¤ì¤Æ¤¤¿ HTTP ¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤ò Mechanize::Headers ¥ª¥Ö¥¸¥§¥¯¥È¤ÇÊÖ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#response ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#body ¤ò¥Ð¥¤¥Ê¥ê¥â¡¼¥É¤Ç¥Õ¥¡¥¤¥ë¤ËÊݸ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#save_as ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
°ú¿ô¤Î¾ò·ï¤Ë³ºÅö¤¹¤ë HTML ¥Î¡¼¥É¤ò #parser ¤«¤éõ¤·¡¢¹çÃפ¹¤ë¤â¤Î¤ò¤¹¤Ù¤ÆÊÖ¤·¤Þ¤¹¡£
parser.search(xpath_or_css) ¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£Mechanize#html_parser ¤¬¥Ñ¡¼¥¹¤ÇÊÖ¤¹¥ª¥Ö¥¸¥§¥¯¥È¤Î search ¥á¥½¥Ã¥É¤Î²òÀâ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¥Ç¥Õ¥©¥ë¥È¥Ñ¡¼¥µ¤Î Nokogiri ¤Î¾ì¹ç¡¢XPath ¤« CSS3 µË¡¤Ç HTML ¤Î¥Î¡¼¥É¸¡º÷¤¬¤Ç¤¤Þ¤¹¡£
<title> ¤Ë°Ï¤Þ¤ì¤¿Ê¸»úÎó¡Êtitle Í×ÁǡˤòÊÖ¤·¤Þ¤¹¡£
<title> ¤½¤Î¤â¤Î¤¬Ìµ¤¤¾ì¹ç¤ä°Ï¤Þ¤ì¤¿Ê¸»úÎ󤬶õ¤Î¾ì¹ç¤Ï nil ¤òÊÖ¤·¤Þ¤¹¡£
¤¿¤À¤Î inner_text ¤Ê¤Î¤Ç¡¢Ê¸»ú»²¾È¤Ï²ò·è¤µ¤ì¤Æ¤¤¤Þ¤¹¤¬Á°¸å¤Î¶õÇòÅù¤Ï HTML ¤Ç½ñ¤«¤ì¤¿¤È¤¤Î¤Þ¤Þ¤Ç¤¹¡£
<html> <title>¥Æ¥¹¥È </title> </html>¤È¤¤¤¦ HTML ¤À¤Ã¤¿¾ì¹ç¡¢title ¥á¥½¥Ã¥É¤Ï "¥Æ¥¹¥È \n" ¤òÊÖ¤·¤Þ¤¹¡£
¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú¤Ï Mechanize#html_parser ¤¬°·¤¦¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¡ÊÄ̾ï UTF-8¡Ë¤ÇÊÖ¤ê¤Þ¤¹¡£<title> ¤¬Ê£¿ô¸ºß¤¹¤ëÉÔÀµ¤Ê HTML ¤Î¾ì¹ç¤Ïñ½ã¤ËÏ¢·ë¤µ¤ì¤¿·ë²Ì¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @title ¤Ëʸ»úÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¤³¤Î¥Õ¥¡¥¤¥ë¤ò¼èÆÀ¤·¤¿ºÝ¤ÎºÇ½ªÅª¤Ê URI ¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#uri ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#parser ¤«¤é base Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Page::Base? ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
base Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @base ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
¾ò·ï¤Ë¤è¤ëÃê½Ð¤Ï #base_with ¤È #bases_with ¤Ç¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¤¬¡¢¥æ¡¼¥¶¡¼¤¬»ÈÍѤ¹¤ëµ¡²ñ¤Ï¤ª¤½¤é¤¯¤Ê¤¤¤Ç¤·¤ç¤¦¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Base? ¥ª¥Ö¥¸¥§¥¯¥È¤ò #bases ¤«¤éõ¤·¡¢ºÇ½é¤Î¤â¤Î¤òÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxx_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
͸ú¤Ê¥á¥½¥Ã¥É¤ÈÃͤΥڥ¢¤Ï Mechanize::Page::Base? ¤Î¥á¥½¥Ã¥É¤ÈÊÖ¤êÃͤˤʤê¤Þ¤¹¡£
{¥á¥½¥Ã¥É̾ => ÊÖ¤êÃÍ} ¤È¤¤¤¦¾ò·ï¤òËþ¤¿¤¹ Mechanize::Page::Base? ¥ª¥Ö¥¸¥§¥¯¥È¤ò #bases ¤«¤éõ¤·¡¢Á´¤Æ¤òÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
Æ°ºî¤Ï Mechanize::Form#xxxxs_with ¤ò¡¢¾ò·ï¤Ë¤Ä¤¤¤Æ¤Ï #base_with ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤Æ gzip Ÿ³«ºÑ¤ß¤Î HTML ¤Ç¤¢¤Ã¤¿¤â¤Î¤ò¡¢°ú¿ô¤Îʸ»úÎó¤ÇÃÖ¤´¹¤¨¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#body= ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
Ruby1.9 ¤Ç¤Ï #parser ÆâÉô¤Î Nokogiri ¤¬ HTML ¤Î Encoding ¤òÆɤळ¤È¤¬¤¢¤ë¤è¤¦¤Ê¤Î¤Ç¡¢#encoding ¤Ç¤ÎÆóÅÙ¼ê´Ö¤òÈò¤±¤ë¤¿¤á¤Ë¤â¤Ç¤¤ì¤ÐÀµ¤·¤¤ Encoding ¤òÀßÄꤷ¤¿Ê¸»úÎó¤ò°ú¿ô¤Ë»ØÄꤷ¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î¥Õ¥¡¥¤¥ë¤ò¥µ¡¼¥Ð¤«¤é¼èÆÀ¤·¤¿ºÝ¤Î¥ê¥¶¥ë¥È¥³¡¼¥É¤ò3·å¤Î¿ô»ú¤Îʸ»úÎó¤ÇºÆÀßÄꤷ¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#code= ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#save_as ¤Ç°ú¿ô¤Ê¤·¤Î¤È¤¤Ë»ÈÍѤµ¤ì¤ëÊݸÍÑ¥Õ¥¡¥¤¥ë¥Ñ¥¹¤òÊÖ¤·¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#filename ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#save_as ¤Ç°ú¿ô¤Ê¤·¤Î¤È¤¤Ë»ÈÍѤµ¤ì¤ëÊݸÍÑ¥Õ¥¡¥¤¥ë¥Ñ¥¹¤òʸ»úÎó¤ÇÀßÄꤷ¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#filename= ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
#parser ¤«¤é img Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Page::Image? ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
img Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @images ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
»ÄÇ°¤Ê¤¬¤é¾ò·ï¤Ë¤è¤ëÃê½Ð¤ò¹Ô¤¦ image_with ¤Î¤è¤¦¤Ê¥á¥½¥Ã¥É¤Ï¤¢¤ê¤Þ¤»¤ó¡£
#images ¤Ë´Þ¤Þ¤ì¤ë Image ¥ª¥Ö¥¸¥§¥¯¥È¤Î Mechanize::Page::Image#url? ¥á¥½¥Ã¥É¤ÎÊÖ¤êÃͤò½¸¤á¡¢½ÅÊ£¤ò¼è¤ê½ü¤¤¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
img Í×ÁǤ¬¥Ú¡¼¥¸¤Ë¤Ê¤¤¾ì¹ç¤ä¡¢src °À¤¬¶õ¤Î img ¤À¤±¤È¤¤¤¦·ù¤¬¤é¤»¤¬¤¢¤Ã¤¿¾ì¹ç¤Ë¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @image_urls ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
»ÄÇ°¤Ê¤¬¤é¾ò·ï¤Ë¤è¤ëÃê½Ð¤ò¹Ô¤¦ image_url_with ¤Î¤è¤¦¤Ê¥á¥½¥Ã¥É¤Ï¤¢¤ê¤Þ¤»¤ó¡£
#parser ¤«¤é label Í×ÁǤò¸¡º÷¤·¡¢½Ð¸½½ç¤Ë Mechanize::Page::Label? ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
label Í×ÁǤ¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
ÆâÉôÊÑ¿ô @labels ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
»ÄÇ°¤Ê¤¬¤é¾ò·ï¤Ë¤è¤ëÃê½Ð¤ò¹Ô¤¦ label_with ¤Î¤è¤¦¤Ê¥á¥½¥Ã¥É¤Ï¤¢¤ê¤Þ¤»¤ó¡£Mechanize ¤¬¥é¥Ù¥ë¤ò¸¡º÷¤¹¤ëºÝ¤Ë»ÈÍѤµ¤ì¤ë¥á¥½¥Ã¥É¤Ç¡¢¥æ¡¼¥¶¡¼¤¬»ÈÍѤ¹¤ë¤³¤È¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¡£
¥é¥Ù¥ëʸ»úÎó¤ÈLabel¥ª¥Ö¥¸¥§¥¯¥È¤ÎÂбþ¤òɽ¤¹ Hash ¤òÊÖ¤·¤Þ¤¹¡£
Mechanize ¤¬¥é¥Ù¥ë¤ò¸¡º÷¤¹¤ëºÝ¤Ë»ÈÍѤµ¤ì¤ë¥á¥½¥Ã¥É¤Ç¡¢¥æ¡¼¥¶¡¼¤¬»ÈÍѤ¹¤ë¤³¤È¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¡£
#parser ¤«¤é meta Í×ÁǤò¸¡º÷¤·¡¢¤½¤ÎÃ椫¤é http-equiv="refresh" ¤Ê¤â¤Î¤ò Mechanize::Page::Meta ¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤·¤ÆÇÛÎó¤ÇÊÖ¤·¤Þ¤¹¡£
¾åµ¤Î¤¤¤ï¤æ¤ë¡Ö¥ê¥Õ¥ì¥Ã¥·¥åÍѤΠmeta ¥¿¥°¡×¤¬Ìµ¤«¤Ã¤¿¾ì¹ç¤Ï¶õ¤ÎÇÛÎó [] ¤òÊÖ¤·¤Þ¤¹¡£
º£¤Î¤È¤³¤í¡Ö¥ê¥Õ¥ì¥Ã¥·¥åÍѤΠmeta ¥¿¥°¡×°Ê³°¤ò²ò¼á¤¹¤ë¤³¤È¤Ï¤¢¤ê¤Þ¤»¤ó¡£¥á¥½¥Ã¥É̾¤â metas ¤Ç¤Ï¤Ê¤¯ meta ¤Ç¤¹¡£
ÆâÉôÊÑ¿ô @meta ¤ËÇÛÎ󤬥¥ã¥Ã¥·¥å¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É»ÈÍѸå¤Ë #parser ¤¬¹¹¿·¤µ¤ì¤Æ¤âÈ¿±Ç¤µ¤ì¤Þ¤»¤ó¡£
»ÄÇ°¤Ê¤¬¤é¾ò·ï¤Ë¤è¤ëÃê½Ð¤ò¹Ô¤¦ meta_with ¤Î¤è¤¦¤Ê¥á¥½¥Ã¥É¤Ï¤¢¤ê¤Þ¤»¤ó¡£
¥µ¡¼¥Ð¤«¤éÁ÷¤é¤ì¤Æ¤¤¿ HTTP ¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤ò Hash ¤« Mechanize::Headers ¤ÇºÆÀßÄꤷ¤Þ¤¹¡£
¥¯¥é¥¹Ê¬¤±¤Ï³ÎÄꤷ¤Æ¤·¤Þ¤Ã¤Æ¤¤¤ë¤Î¤Ç¡¢¤³¤Î¥á¥½¥Ã¥É¤Ç¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤òÊѹ¹¤·¤Æ¤â Mechanize ¤ÎµóÆ°¤ËÊѲ½¤Ï¤¢¤ê¤Þ¤»¤ó¡£
·Ñ¾µ¸µ¤Î Mechanize::File#response= ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î¥Õ¥¡¥¤¥ë¤ò¼èÆÀ¤·¤¿ºÝ¤ÎºÇ½ªÅª¤Ê URI ¥ª¥Ö¥¸¥§¥¯¥È¤òºÆÀßÄꤷ¤Þ¤¹¡£
·Ñ¾µ¸µ¤Î Mechanize::File#uri= ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£