{"id":730,"date":"2016-11-12T11:13:41","date_gmt":"2016-11-12T02:13:41","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=730"},"modified":"2019-10-19T11:54:58","modified_gmt":"2019-10-19T02:54:58","slug":"blog81","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog81\/","title":{"rendered":"(81) \u91ce\u7403\u9078\u624b\u306e\u6210\u7e3e\u3092\u4e3b\u6210\u5206\u5206\u6790 (\u7d9a\u7de81\/2)"},"content":{"rendered":"<p><a href=\"https:\/\/www.dogrow.net\/python\/blog80\/\" target=_blank rel=\"noopener noreferrer\">(80) \u91ce\u7403\u9078\u624b\u3092\u6e2c\u3063\u3066\u307f\u308b 2016\u5e74\u7248<\/a> \u3067\u306f\u3001\u65e5\u672c\u30d7\u30ed\u91ce\u74032016\u5e74\u30b7\u30fc\u30ba\u30f3\u306e\u6253\u8005\u7d50\u679c\u3092\u4f7f\u3063\u3066\u904a\u3093\u3060\u3002\u3069\u3046\u305b\u3084\u308b\u306e\u306a\u3089\u3070\u3001\u3082\u3063\u3068\u5927\u304d\u306a\u30c7\u30fc\u30bf\u3067\u540c\u3058\u3053\u3068\u3092\u3084\u3063\u3066\u307f\u305f\u3044\u3001\u30d7\u30ed\u91ce\u7403\u306e\u904e\u53bb\u306e\u7d50\u679c\u3082\u4f7f\u3063\u3066\u540c\u3058\u89e3\u6790\u3092\u3057\u3066\u307f\u305f\u3044\u3001\u3068\u601d\u3046\u3002<\/p>\n<p>\u3057\u304b\u3057&#8230;<br \/>\n\u524d\u56de\u306fYahoo\u30b9\u30dd\u30fc\u30c4\u306e\u8868\u793a\u60c5\u5831\u3092\u624b\u52d5\u3067\u30b3\u30d4\u30da\u3057\u3066\u5165\u529b\u7528 CSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u305f\u304c\u3001\u3053\u306e\u624b\u4f5c\u696d\u3092\u904e\u53bb\u4f55\u5e74\u5206\u3082\u3084\u308b\u3068\u91cf\u304c\u591a\u3059\u304e\u3066\u9014\u4e2d\u3067\u6c17\u6301\u3061\u304c\u5207\u308c\u3061\u3083\u3044\u305d\u3046\u3060\u3002<\/p>\n<p>\u305d\u3053\u3067&#8230;<br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/blog75\/\" target=_blank rel=\"noopener noreferrer\">(75) \u6307\u5b9aURL\u306eWEB\u30da\u30fc\u30b8\u306eHTML\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u8868\u793a<\/a> \u3084<br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/blog79\/\" target=_blank rel=\"noopener noreferrer\">(79) \u306a\u3093\u3061\u3083\u3063\u3066Google\u691c\u7d22<\/a><br \/>\n\u3067\u4f7f\u3063\u305f\u30c6\u30af\u30cb\u30c3\u30af\u3092\u5229\u7528\u3057\u3066\u3001\u904e\u53bb\u30c7\u30fc\u30bf\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u81ea\u52d5\u53ce\u96c6\u3055\u305b\u3066\u307f\u305f\u3044\u3002<\/p>\n<h1 class=\"my_h\">\u904e\u53bb\u30c7\u30fc\u30bf\u3092\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0<\/h1>\n<p>\u524d\u8ff0\u306e(79)\u3067\u306f\u3001\u53d6\u5f97\u3057\u305fHTML\u306e\u69cb\u6587\u89e3\u6790\u306b <span class=\"my_fc_deeppinkB\">HTMLParser<\/span> \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u305f\u3002<br \/>\n\u4eca\u56de\u306f\u697d\u3092\u3059\u308b\u305f\u3081\u306b <span class=\"my_fc_deeppinkB\">lxml<\/span> \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3046\u3002C,C++\u3067\u306e\u958b\u767a\u3067 libxml\u3092\u9577\u3044\u3053\u3068\u4f7f\u3063\u3066\u3044\u308b\u304c\u3001\u3053\u308c\u3092\u4f7f\u3046\u3068 XPath\u3067\u3061\u3087\u3061\u3087\u3044\u3068\u5404\u30bf\u30b0\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u3060\u3002<\/p>\n<p>\u307e\u305a\u306f Python\u306b lxml\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u304f\u3002<\/p>\n<pre>\r\n$ easy_install --user lxml\r\n$ easy_install --user cssselect\r\n<\/pre>\n<h2 class=\"my_h\">1) \u30c7\u30fc\u30bf\u53ce\u96c6\uff06\u4fdd\u5b58<\/h2>\n<p><a href=\"http:\/\/npb.jp\/\" target=_blank rel=\"noopener noreferrer\">NPB(\u65e5\u672c\u91ce\u7403\u6a5f\u69cb)<\/a>\u69d8\u306e\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u3067\u306f\u30012005\u5e74\uff5e2016\u5e74\u306e\u6253\u8005\u6210\u7e3e\u304c\u63b2\u8f09\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u3053\u308c\u3092\u30c7\u30fc\u30bf\u89e3\u6790\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3068\u3057\u3066\u4f7f\u308f\u305b\u3066\u3044\u305f\u3060\u304f\u30022005\u5e74\uff5e2016\u5e74\u306e12\u5e74\u5206\u00d72\u30ea\u30fc\u30b0\u5206\u3001\u516824\u30da\u30fc\u30b8\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u308f\u305b\u3066\u3044\u305f\u3060\u304f\u3002<\/p>\n<h3 class=\"my_h\">index.cgi<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/local\/bin\/python\r\n# -*- coding: utf-8 -*-\r\n\r\nprint 'Content-type: text\/html'\r\n\r\nimport npbparser\r\nimport numpy as np\r\n\r\nprint &quot;&quot;&quot;\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot; \/&gt;\r\n&lt;title&gt;0081&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&quot;&quot;&quot;\r\n\r\ndef collect_data(year, league_cp):\r\n\turl = &quot;http:\/\/npb.jp\/bis\/%d\/stats\/bat_%s.html&quot; % (year, league_cp)\r\n\tfname = &quot;%d_%s.csv&quot; % (year, league_cp)\r\n\tres = npbparser.get_npb_score(url);\r\n\tnp.savetxt( fname, res, delimiter=',',fmt='%s')\r\n\r\nfor year in range(2005,2016):\r\n\tcollect_data(year,'c')\r\n\tcollect_data(year,'p')\r\n\r\nprint &quot;&quot;&quot;\r\n&lt;form enctype=&quot;multipart\/form-data&quot; action=&quot;.\/&quot; method=post&gt;\r\n&lt;input type=submit value=&quot;\u5b9f\u884c&quot;&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n&quot;&quot;&quot;\r\n<\/pre>\n<h3 class=\"my_h\">npbparser.py<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!~\/usr\/local\/bin\/python\r\n# -*- coding: utf-8 -*-\r\nimport numpy as np\r\nimport urllib2\r\nimport lxml.html\r\nimport chardet\r\n\r\ndef get_npb_score(url):\r\n    #------------------------------------------------\r\n    # load html\r\n    opener = urllib2.build_opener()\r\n    opener.addheaders = &#x5B;('User-agent', 'Mozilla\/5.0')]\r\n    res = opener.open(url)\r\n    html = res.read()\r\n    res.close()\r\n    ccode = chardet.detect(html)          # \u6587\u5b57\u30b3\u30fc\u30c9\u3092\u5224\u5b9a\r\n    html = html.decode(ccode&#x5B;'encoding']) # unicode\u306b\u5909\u63db\r\n\r\n    #------------------------------------------------\r\n    # parse HTML\r\n    dom = lxml.html.fromstring(html)      # DOM\u30c4\u30ea\u30fc\u751f\u6210\r\n    ary_tr = dom.xpath(&quot;\/\/div&#x5B;@id='stdivmaintbl']\/\/table\/tr&quot;)\r\n    ary_1player = &#x5B;]\r\n    for tr in ary_tr:\r\n        if len(tr) &amp;lt; 25:\r\n            continue\r\n        da_items = &#x5B;]\r\n        for td in tr:\r\n            da_items.append(td.text.encode(&quot;utf-8&quot;))\r\n        ary_1player.append(da_items)\r\n    ary_np = np.array(ary_1player)\r\n    return ary_np\r\n<\/pre>\n<h2 class=\"my_h\">2) \u5b9f\u884c\u7d50\u679c<\/h2>\n<p>\u904e\u53bb12\u5e74\u5206\u306eCSV\u30d5\u30a1\u30a4\u30eb\u30c7\u30fc\u30bf\u304c\u3067\u304d\u305f\u3002<br \/>\n<span class=\"my_fc_crimsonB\">\u203bUTF-8\u306a\u306e\u3067Excel\u3067\u958b\u304f\u3068\u6587\u5b57\u5316\u3051\u3059\u308b\u304b\u3082<\/span><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2005_c.csv\">2005\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2005_p.csv\">2005\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2006_c.csv\">2006\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2006_p.csv\">2006\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2007_c.csv\">2007\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2007_p.csv\">2007\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2008_c.csv\">2008\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2008_p.csv\">2008\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2009_c.csv\">2009\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2009_p.csv\">2009\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2010_c.csv\">2010\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2010_p.csv\">2010\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2011_c.csv\">2011\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2011_p.csv\">2011\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2012_c.csv\">2012\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2012_p.csv\">2012\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2013_c.csv\">2013\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2013_p.csv\">2013\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2014_c.csv\">2014\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2014_p.csv\">2014\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2015_c.csv\">2015\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2015_p.csv\">2015\u5e74\u30d1<\/a><br \/>\n<a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2016_c.csv\">2016\u5e74\u30bb<\/a> , <a href=\"https:\/\/www.dogrow.net\/python\/sample\/0081\/data\/2016_p.csv\">2016\u5e74\u30d1<\/a><\/p>\n<p>\u6b21\u56de <a href=\"https:\/\/www.dogrow.net\/python\/blog82\/\" target=\"_blank\" rel=\"noopener noreferrer\">(82) \u91ce\u7403\u9078\u624b\u306e\u6210\u7e3e\u3092\u4e3b\u6210\u5206\u5206\u6790 (\u7d9a\u7de82\/2)<\/a> \u3067\u306f\u3001\u3053\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066PCA\u3057\u3066\u307f\u308b\u3002<\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>(80) \u91ce\u7403\u9078\u624b\u3092\u6e2c\u3063\u3066\u307f\u308b 2016\u5e74\u7248 \u3067\u306f\u3001\u65e5\u672c\u30d7\u30ed\u91ce\u74032016\u5e74\u30b7\u30fc\u30ba\u30f3\u306e\u6253\u8005\u7d50\u679c\u3092\u4f7f\u3063\u3066\u904a\u3093\u3060\u3002\u3069\u3046\u305b\u3084\u308b\u306e\u306a\u3089\u3070\u3001\u3082\u3063\u3068\u5927\u304d\u306a\u30c7\u30fc\u30bf\u3067\u540c\u3058\u3053\u3068\u3092\u3084\u3063\u3066\u307f\u305f\u3044\u3001\u30d7\u30ed\u91ce\u7403\u306e\u904e\u53bb\u306e\u7d50\u679c\u3082\u4f7f\u3063\u3066\u540c\u3058\u89e3\u6790\u3092\u3057\u3066\u307f\u305f\u3044\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog81\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,29],"tags":[],"class_list":["post-730","post","type-post","status-publish","format-standard","hentry","category-web","category-29"],"views":5199,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/comments?post=730"}],"version-history":[{"count":35,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/730\/revisions"}],"predecessor-version":[{"id":2720,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/730\/revisions\/2720"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}