{"id":784,"date":"2018-09-09T21:21:47","date_gmt":"2018-09-09T12:21:47","guid":{"rendered":"https:\/\/yuis-programming.com\/?p=784"},"modified":"2021-11-17T13:45:41","modified_gmt":"2021-11-17T04:45:41","slug":"ruby%e3%81%a7mechanize%e3%81%a7%e3%83%96%e3%83%ad%e3%82%b0%e3%82%92%e3%82%ad%e3%83%bc%e3%83%af%e3%83%bc%e3%83%89%e6%a4%9c%e7%b4%a2%e3%81%97%e3%81%a6%e6%9c%80%e5%88%9d%e3%81%ae%e6%8a%95%e7%a8%bf","status":"publish","type":"post","link":"https:\/\/yuis-programming.com\/?p=784","title":{"rendered":"Ruby\u306emechanize\u3068nokogiri\u3067\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0"},"content":{"rendered":"<p>\u79c1\u306e\u30d6\u30ed\u30b0 &#8220;https:\/\/yuis-programming.com\/&#8221; \u3067\u3001\u8a18\u4e8b\u3092\u691c\u7d22\u3057\u3066\u6700\u521d\u306b\u51fa\u3066\u304d\u305f\u8a18\u4e8b\u306e\u30bf\u30a4\u30c8\u30eb\u3092\u53d6\u5f97\u3059\u308b\u3001\u3068\u3044\u3046\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nRuby\u306emechanize\u3068nokogiri\u306e\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u306e\u4f7f\u7528\u4f8b\u3068\u3057\u3066\u3001Ruby\u306emechanize\u3067\u30d6\u30ed\u30b0\u3092\u30ad\u30fc\u30ef\u30fc\u30c9\u691c\u7d22\u3057\u3066\u6700\u521d\u306e\u6295\u7a3f\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u30e1\u30e2\u3067\u3059\u3002<br \/>\nRuby\u306e\u30e9\u30a4\u30d6\u30e9\u30eamechanize\u3068nokogiri\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u3084\u3063\u3066\u308b\u3053\u3068\uff1a<br \/>\n1. \u691c\u7d22\u7a93\u3092\u30af\u30ea\u30c3\u30af\u3067\u8868\u793a<br \/>\n1. \u30d5\u30a9\u30fc\u30e0\u306b\u691c\u7d22\u3057\u305f\u3044\u5024\u3092\u5b9a\u7fa9<br \/>\n1. \u30d5\u30a9\u30fc\u30e0\u3092\u9001\u4fe1<br \/>\n1. \u691c\u7d22\u7d50\u679c\u306eHTML\u3092nokogiri\u3067\u30d1\u30fc\u30b9\u3057\u3066\u30bf\u30a4\u30c8\u30eb\u3092\u53d6\u5f97<\/p>\n<p>\u4ee5\u4e0b\u4f8b\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<pre><code>require 'mechanize'\nrequire 'logger'\n\nagent = Mechanize.new\nagent.log = Logger.new \"mech.log\"\nagent.user_agent_alias = 'Mac Safari'\n\npage = agent.get \"https:\/\/yuis-programming.com\/\"\n\npage.link_with(:xpath =&gt; '\/\/*[@id=\"menu-navigator\"]\/li[4]\/a').click\npage.form.field_with(xpath: '\/\/*[@id=\"s\"]').value = 'magic'\n# page.form.field_with(xpath: '\/\/*[@id=\"s\"]').value = 'ruby'\nsearch_results = agent.submit page.form_with(xpath: '\/\/*[@id=\"searchform\"]')\n\nrequire 'nokogiri'\nrequire 'open-uri'\n\nEncoding.default_external = 'UTF-8'\n\ndoc = Nokogiri::HTML(search_results.body)\nputs firstpost = doc.xpath('\/\/div\/div\/header\/h2\/a')[0].text\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u79c1\u306e\u30d6\u30ed\u30b0 &#8220;https:\/\/yuis-programming.com\/&#8221; \u3067\u3001\u8a18\u4e8b\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"acf":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8Rxwo-cE","_links":{"self":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/784"}],"collection":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=784"}],"version-history":[{"count":3,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/784\/revisions"}],"predecessor-version":[{"id":3437,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/784\/revisions\/3437"}],"wp:attachment":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}