{"id":299,"date":"2014-02-20T10:02:55","date_gmt":"2014-02-20T01:02:55","guid":{"rendered":"http:\/\/lovelinux.mydns.jp\/?p=299"},"modified":"2014-03-21T17:27:19","modified_gmt":"2014-03-21T08:27:19","slug":"ruby%e3%81%a7web%e3%83%9a%e3%83%bc%e3%82%b8%e3%81%8b%e3%82%89%e3%83%88%e3%83%94%e3%83%83%e3%82%af%e4%b8%80%e8%a6%a7%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e3%81%a6%e3%81%bf%e3%82%88%e3%81%86%e3%80%82-mec","status":"publish","type":"post","link":"https:\/\/lovelinux.mydns.jp\/?p=299","title":{"rendered":"Ruby\u3067Web\u30da\u30fc\u30b8\u304b\u3089\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u3092\u53d6\u5f97\u3057\u3066\u307f\u3088\u3046\u3002 Mechanize\/nokogiri"},"content":{"rendered":"<p>Web\u30da\u30fc\u30b8\u304b\u3089\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u3092\u5f15\u3063\u3053\u629c\u304f\u30c6\u30af\u30cb\u30c3\u30af\u3067\u3059\u3002\u4e00\u822c\u7684\u306b\u306f\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3068\u8a00\u308f\u308c\u308b\u3089\u3057\u3044\u3067\u3059\u304c\u3001\u540d\u524d\u306f\u3055\u3066\u304a\u304d\u3001\u9069\u5f53\u306a\u30da\u30fc\u30b8\u304b\u3089\u30cb\u30e5\u30fc\u30b9\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u306a\u3093\u304b\u3092\u5f15\u3063\u3053\u629c\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u6d45\u7530\u771f\u592e\u6b8b\u5ff5\uff01\u30d5\u30ea\u30fc\u3067\u633d\u56de\u3067\u304d\u308b\u304b\u306a\u3041\uff1f\uff1f<\/p>\n<h4>\u4e0b\u6e96\u5099<\/h4>\n<p>Ruby\u306egem nokogiri\u3068mechanize\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>[bash]<\/p>\n<p>$ gem install nokogiri<br \/>\n$ gem install mechanize<\/p>\n<p>[\/bash]<\/p>\n<h4>\u53d6\u5f97\u5bfe\u8c61\u3068\u3059\u308b\u30da\u30fc\u30b8<\/h4>\n<p>\u4eca\u56de\u306f\u671d\u65e5\u65b0\u805e\u3055\u3093\u306e\u30b5\u30a4\u30c8\u304b\u3089\u30c8\u30d4\u30c3\u30af\u3092\u629c\u304d\u53d6\u3063\u3066\u307f\u307e\u3059\u3002\u8d64\u304f\u56f2\u3093\u3060\u3068\u3053\u308d\u304c\u30bf\u30fc\u30b2\u30c3\u30c8\u3067\u3059\u3002<\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-1.png\"><img decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-1.png\" alt=\"mechanize-1\" width=\"650\" class=\"alignnone size-large wp-image-300\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-1.png 1052w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-1-300x181.png 300w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-1-1024x618.png 1024w\" sizes=\"(max-width: 1052px) 100vw, 1052px\" \/><\/a><\/p>\n<h4>HTML\u306e\u69cb\u9020\u3092\u76ee\u8996\u3067\u30c1\u30a7\u30c3\u30af<\/h4>\n<p>\u30c8\u30d4\u30c3\u30af\u6b04\u306eHTML\u306e\u69cb\u9020\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u3069\u3093\u306aCSS\u30bb\u30ec\u30af\u30bf\u30fc\u3067\u4e00\u89a7\u3092\u5f15\u3063\u3053\u629c\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u4eca\u56de\u306e\u5834\u5408\u306f\u30af\u30e9\u30b9List\/ListSideImage\/ListHeadline\u3092\u3082\u3064UL\u30bf\u30b0\u306e\u4e0b\u306b\u5b58\u5728\u3059\u308b a\u30bf\u30b0\u306e\u60c5\u5831\u3092\u629c\u3051\u3070\u826f\u3044\u3068\u3044\u3046\u306e\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002<br \/>\n<a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-2.png\"><img decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-2.png\" alt=\"mechanize-2\" width=\"650\"  class=\"alignnone size-large wp-image-301\" srcset=\"https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-2.png 737w, https:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2014\/02\/mechanize-2-300x199.png 300w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/a><\/p>\n<h4>Ruby\u306e\u30bd\u30fc\u30b9<\/h4>\n<p>test.rb\u3000\u3068\u304b\u3000\u9069\u5f53\u306a\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4fdd\u5b58\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>[ruby]<br \/>\nrequire &#8216;mechanize&#8217;<\/p>\n<p>client = Mechanize.new<br \/>\nurl = &#8220;http:\/\/www.asahi.com&#8221;<\/p>\n<p>page = client.get(url)<\/p>\n<p>atags = page.search(&#8220;ul.List.ListSideImage.ListHeadline a&#8221;)<\/p>\n<p>p atags.length<br \/>\natags.each do |atag|<br \/>\n\tp atag.inner_text<br \/>\nend<\/p>\n<p>[\/ruby]<\/p>\n<h4>\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u53d6\u5f97<\/h4>\n<p>\u3055\u3066\u3001\u30bd\u30fc\u30b9\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff01<\/br><br \/>\n\u3081\u3067\u305f\u304f\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u306e\u53d6\u5f97\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<p>[bash]<br \/>\n$ ruby test.rb<br \/>\n7<br \/>\n&#8220;\u30d5\u30a3\u30ae\u30e5\u30a2\uff13\u4eba\u5a18\u306b\u4f55\u304c\u2026\u3000\u3053\u3068\u3054\u3068\u304f\u30df\u30b9\u3001\u8ca0\u306e\u9023\u9396(08:22)&#8221;<br \/>\n&#8220;\u307e\u3055\u304b\u306e\uff33\uff30\uff11\uff16\u4f4d&#8221;<br \/>\n&#8220;\u771f\u592e\u3001\u9707\u3048\u308b\u58f0\u3000\u300c\u81ea\u5206\u304c\u5f31\u304b\u3063\u305f\u300d\u3000\u5fc3\u6280\u4f53\u6574\u308f\u305a\u6697\u8ee2(08:23)&#8221;<br \/>\n&#8220;\u9234\u6728\u3001\u60c5\u611f\u305f\u3063\u3077\u308a\u306b\u821e\u3046\u3000\u30df\u30b9\u306b\u306f\u300c\u6094\u3057\u3044\u300d(08:57)&#8221;<br \/>\n&#8220;\u30c8\u30e8\u30bf\u3001\uff16\u5e74\u3076\u308a\u306b\u30d9\u30a2\u5b9f\u65bd\u3078\u3000\u4e2d\u5c0f\u4f01\u696d\u306b\u6ce2\u53ca\u3059\u308b\u304b(07:58)&#8221;<br \/>\n&#8220;\uff35\uff33\uff2a\u3001\u65b0\u30c6\u30fc\u30de\u30d1\u30fc\u30af\u5efa\u8a2d\u3078\u3000\u6771\u5317\u30fb\u4e5d\u5dde\u306a\u3069\u5019\u88dc\u304b(06:32)&#8221;<br \/>\n&#8220;\u4e2d\u56fd\u3001\u5916\u56fd\u8a18\u8005\u306b\u5bfe\u65e5\u5ba3\u4f1d\u6226\u3000\u5357\u4eac\u6848\u5185\u3001\u56fd\u969b\u4e16\u8ad6\u306b\u8a34\u3048(09:50)&#8221;<br \/>\n[\/bash]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web\u30da\u30fc\u30b8\u304b\u3089\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u3092\u5f15\u3063\u3053\u629c\u304f\u30c6\u30af\u30cb\u30c3\u30af\u3067\u3059\u3002\u4e00\u822c\u7684\u306b\u306f\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3068\u8a00\u308f\u308c\u308b\u3089\u3057\u3044\u3067\u3059\u304c\u3001\u540d\u524d\u306f\u3055\u3066\u304a\u304d\u3001\u9069\u5f53\u306a\u30da\u30fc\u30b8\u304b\u3089\u30cb\u30e5\u30fc\u30b9\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u306a\u3093\u304b\u3092\u5f15\u3063\u3053\u629c\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u6d45\u7530\u771f\u592e\u6b8b\u5ff5\uff01\u30d5\u30ea\u30fc\u3067\u633d\u56de\u3067\u304d\u308b\u304b\u306a\u3041\uff1f\uff1f \u4e0b\u6e96\u5099 Ruby\u306egem nokogiri\u3068mechanize\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002 [bash] $ gem install nokogiri $ gem install mechanize [\/bash] \u53d6\u5f97\u5bfe\u8c61\u3068\u3059\u308b\u30da\u30fc\u30b8 \u4eca\u56de\u306f\u671d\u65e5\u65b0\u805e\u3055\u3093\u306e\u30b5\u30a4\u30c8\u304b\u3089\u30c8\u30d4\u30c3\u30af\u3092\u629c\u304d\u53d6\u3063\u3066\u307f\u307e\u3059\u3002\u8d64\u304f\u56f2\u3093\u3060\u3068\u3053\u308d\u304c\u30bf\u30fc\u30b2\u30c3\u30c8\u3067\u3059\u3002 HTML\u306e\u69cb\u9020\u3092\u76ee\u8996\u3067\u30c1\u30a7\u30c3\u30af \u30c8\u30d4\u30c3\u30af\u6b04\u306eHTML\u306e\u69cb\u9020\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u3069\u3093\u306aCSS\u30bb\u30ec\u30af\u30bf\u30fc\u3067\u4e00\u89a7\u3092\u5f15\u3063\u3053\u629c\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u4eca\u56de\u306e\u5834\u5408\u306f\u30af\u30e9\u30b9List\/ListSideImage\/ListHeadline\u3092\u3082\u3064UL\u30bf\u30b0\u306e\u4e0b\u306b\u5b58\u5728\u3059\u308b a\u30bf\u30b0\u306e\u60c5\u5831\u3092\u629c\u3051\u3070\u826f\u3044\u3068\u3044\u3046\u306e\u304c\u308f\u304b\u308a\u307e\u3057\u305f\u3002 Ruby\u306e\u30bd\u30fc\u30b9 test.rb\u3000\u3068\u304b\u3000\u9069\u5f53\u306a\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4fdd\u5b58\u3057\u3066\u304f\u3060\u3055\u3044\u3002 [ruby] require &#8216;mechanize&#8217; client = Mechanize.new url = &#8220;http:\/\/www.asahi.com&#8221; page = client.get(url) atags = page.search(&#8220;ul.List.ListSideImage.ListHeadline a&#8221;) p atags.length atags.each do |atag| p atag.inner_text end [\/ruby] \u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u53d6\u5f97 \u3055\u3066\u3001\u30bd\u30fc\u30b9\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff01 \u3081\u3067\u305f\u304f\u30c8\u30d4\u30c3\u30af\u4e00\u89a7\u306e\u53d6\u5f97\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002\u7c21\u5358\u3067\u3059\u306d\u3002 [bash] $ ruby test.rb 7 &#8220;\u30d5\u30a3\u30ae\u30e5\u30a2\uff13\u4eba\u5a18\u306b\u4f55\u304c\u2026\u3000\u3053\u3068\u3054\u3068\u304f\u30df\u30b9\u3001\u8ca0\u306e\u9023\u9396(08:22)&#8221; &#8220;\u307e\u3055\u304b\u306e\uff33\uff30\uff11\uff16\u4f4d&#8221; &#8220;\u771f\u592e\u3001\u9707\u3048\u308b\u58f0\u3000\u300c\u81ea\u5206\u304c\u5f31\u304b\u3063\u305f\u300d\u3000\u5fc3\u6280\u4f53\u6574\u308f\u305a\u6697\u8ee2(08:23)&#8221; &#8220;\u9234\u6728\u3001\u60c5\u611f\u305f\u3063\u3077\u308a\u306b\u821e\u3046\u3000\u30df\u30b9\u306b\u306f\u300c\u6094\u3057\u3044\u300d(08:57)&#8221; &#8220;\u30c8\u30e8\u30bf\u3001\uff16\u5e74\u3076\u308a\u306b\u30d9\u30a2\u5b9f\u65bd\u3078\u3000\u4e2d\u5c0f\u4f01\u696d\u306b\u6ce2\u53ca\u3059\u308b\u304b(07:58)&#8221; &#8220;\uff35\uff33\uff2a\u3001\u65b0\u30c6\u30fc\u30de\u30d1\u30fc\u30af\u5efa\u8a2d\u3078\u3000\u6771\u5317\u30fb\u4e5d\u5dde\u306a\u3069\u5019\u88dc\u304b(06:32)&#8221; &#8220;\u4e2d\u56fd\u3001\u5916\u56fd\u8a18\u8005\u306b\u5bfe\u65e5\u5ba3\u4f1d\u6226\u3000\u5357\u4eac\u6848\u5185\u3001\u56fd\u969b\u4e16\u8ad6\u306b\u8a34\u3048(09:50)&#8221; [\/bash]<\/p>\n","protected":false},"author":1,"featured_media":392,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6,2],"tags":[],"class_list":{"0":"post-299","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-rails","8":"category-2"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":2,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/media\/392"}],"wp:attachment":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}