{"id":1032,"date":"2017-08-04T04:40:50","date_gmt":"2017-08-03T19:40:50","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=1032"},"modified":"2019-10-19T12:06:07","modified_gmt":"2019-10-19T03:06:07","slug":"blog87","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog87\/","title":{"rendered":"(87) NPB\u30d7\u30ed\u91ce\u7403\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u53d6\u5f97\u3059\u308b\u3002(ver.2)"},"content":{"rendered":"<p>\u524d\u56de\u306e <a href=\"https:\/\/www.dogrow.net\/python\/blog86\/\" target=\"_blank\" rel=\"noopener noreferrer\">(86) NPB\u30d7\u30ed\u91ce\u7403\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u53d6\u5f97\u3059\u308b\u3002<\/a> \u3067\u306f\u3001\u4e00\u4eba\u306e\u9078\u624b\u306e\u7d39\u4ecb\u30da\u30fc\u30b8\u306eURL\u3092\u5165\u529b\u3057\u3001\u4e00\u4eba\u306e\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u51fa\u529b\u3059\u308b\u3060\u3051\u306e\u6a5f\u80fd\u3060\u3063\u305f\u3002\u6570\u767e\u4eba\u3082\u306e\u30d7\u30ed\u91ce\u7403\u9078\u624b\u306b\u3064\u3044\u3066\u3053\u308c\u3092\u624b\u4f5c\u696d\u3067\u7e70\u308a\u8fd4\u3059\u306e\u306f\u5927\u5909\u3060\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001NPB\u69d8\u30b5\u30a4\u30c8\u306e\u30c1\u30fc\u30e0\u30e1\u30f3\u30d0\u30fc\u4e00\u89a7\u30da\u30fc\u30b8\u306eURL\u3092\u5165\u529b\u3057\u3001\u305d\u3053\u304b\u3089\u500b\u3005\u306e\u9078\u624b\u7d39\u4ecb\u30da\u30fc\u30b8\u306eURL\u3092\u62bd\u51fa\u3057\u3001\u500b\u3005\u306e\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u51fa\u529b\u3059\u308b\u3088\u3046\u306b\u6539\u826f\u3059\u308b\u3002\u3059\u306a\u308f\u3061\u3001\u30c1\u30fc\u30e0\u3092\u6307\u5b9a\u3059\u308c\u3070\u6240\u5c5e\u30e1\u30f3\u30d0\u30fc\u5168\u54e1\u5206\u306e\u30c7\u30fc\u30bf\u304c\u81ea\u52d5\u3067\u51fa\u529b\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<p><a href=\"http:\/\/npb.jp\/bis\/teams\/rst_h.html\" target=_blank rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/python\/wp-content\/uploads\/2017\/08\/Image2.png\" alt=\"\" class=\"my_img\" \/><\/a><\/p>\n<h1 class=\"my_h\">1. \u4ed5\u69d8<\/h1>\n<p>\u30fbNPB\u69d8\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306e\uff11\u30c1\u30fc\u30e0\u306e\u9078\u624b\u4e00\u89a7\u30da\u30fc\u30b8\u306eURL\u3092\u5165\u529b\u3059\u308b\u3002<br \/>\n\u30fb\u305d\u306e\u30da\u30fc\u30b8\u304b\u3089\u9078\u624b\u500b\u3005\u306e\u7d39\u4ecb\u30da\u30fc\u30b8\u306eURL\u3092\u81ea\u52d5\u62bd\u51fa\u3059\u308b\u3002<br \/>\n\u30fb\u9078\u624b\u500b\u3005\u306e\u7d39\u4ecb\u30da\u30fc\u30b8\u304b\u3089\u5e74\u5225\u6210\u7e3e\u3092\u81ea\u52d5\u62bd\u51fa\u3059\u308b\u3002<br \/>\n\u30fb\u6253\u8005\u6210\u7e3e\u3001\u6295\u624b\u6210\u7e3e\u306b\u5206\u3051\u3066CSV\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u52d5\u51fa\u529b\u3059\u308b\u3002<\/p>\n<h1 class=\"my_h\">2. \u30d7\u30ed\u30b0\u30e9\u30e0<\/h1>\n<p>\u3061\u3087\u3063\u3068\u529b\u305a\u304f\u306a\u3068\u3053\u308d\u3082\u3042\u308b\u304c\u5f8c\u3067\u6539\u826f\u3057\u3088\u3046&#8230;<\/p>\n<h3 class=\"my_h\">\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5165\u529b<\/h3>\n<p>\u4ee5\u4e0b\u306e\u64cd\u4f5c\u3067\u30db\u30fc\u30af\u30b9\u306e\u5168\u9078\u624b\u306e\u5e74\u5ea6\u5225\u6295\u624b\u6210\u7e3e\u3001\u5e74\u5ea6\u5225\u6253\u8005\u6210\u7e3e\u304c\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3055\u308c\u308b\u3002<\/p>\n<pre>\r\n$ mkdir .\/da\r\n$ mkdir .\/pt\r\n$ python3\r\n\r\n>>> import lib_npb\r\n>>> lib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_h.html\", \"H\")\r\n<\/pre>\n<p>\u4ed6\u306e\u30c1\u30fc\u30e0\u306eURL\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\n\u30bb<br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_c.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_c.html \u5e83\u5cf6<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_g.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_g.html \u5de8\u4eba<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_db.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_db.html \u6a2a\u6d5c<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_t.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_t.html \u962a\u795e<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_s.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_s.html \u30e4\u30af\u30eb\u30c8<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_d.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_d.html \u4e2d\u65e5<\/a><br \/>\n\u30d1<br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_h.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_h.html \u30bd\u30d5\u30c8\u30d0\u30f3\u30af<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_f.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_f.html \u65e5\u30cf\u30e0<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_m.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_m.html \u30ed\u30c3\u30c6<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_l.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_l.html \u897f\u6b66<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_e.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_e.html \u697d\u5929<\/a><br \/>\n<a href=\"http:\/\/npb.jp\/bis\/teams\/rst_bs.html\" target=_blank rel=\"noopener noreferrer\">http:\/\/npb.jp\/bis\/teams\/rst_bs.html \u30aa\u30ea\u30c3\u30af\u30b9<\/a><\/p>\n<h3 class=\"my_h\">lib_npb.py<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport urllib.request\r\nfrom bs4 import BeautifulSoup\r\nimport csv\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nclass NpbPlayer:\r\n    #------------------------------------------------------------\r\n    def __init__(self, name, url):\r\n        self.name = name\r\n        self.url  = &quot;http:\/\/npb.jp&quot; + url\r\n        self.ary_record_pitch = &#x5B;]    # \u6295\u624b\u8a18\u9332\r\n        self.ary_record_bat = &#x5B;]      # \u6253\u8005\u8a18\u9332\r\n\r\n    #------------------------------------------------------------\r\n    # 1\u9078\u624b\u306e\u6295\u624b\u8a18\u9332\u3092CSV\u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\r\n    def output_record_to_csv_file_pitch(self, fname):\r\n        if len(self.ary_record_pitch) &gt; 0 :\r\n            with open(fname, &quot;w&quot;) as fh:\r\n                writer = csv.writer(fh, lineterminator=&quot;\\n&quot;)\r\n                writer.writerows(self.ary_record_pitch)\r\n\r\n    #------------------------------------------------------------\r\n    # 1\u9078\u624b\u306e\u6253\u8005\u8a18\u9332\u3092CSV\u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\r\n    def output_record_to_csv_file_bat(self, fname):\r\n        if len(self.ary_record_bat) &gt; 0 :\r\n            with open(fname, &quot;w&quot;) as fh:\r\n                writer = csv.writer(fh, lineterminator=&quot;\\n&quot;)\r\n                writer.writerows(self.ary_record_bat)\r\n\r\n    #------------------------------------------------------------\r\n    # 1\u9078\u624b\u306e\u8a18\u9332\u3092\u53ce\u96c6\r\n    def get_record(self):\r\n        try:\r\n            resp = urllib.request.urlopen(self.url)\r\n        except urllib.error.HTTPError as e:\r\n            print(&quot;HTTP-Error : &quot;, e.code)\r\n            return False\r\n        except urllib.error.URLError as e:\r\n            print(&quot;URL-Error : &quot;, e.reason)\r\n            return False\r\n        else:\r\n            self.ary_record_pitch = &#x5B;]\r\n            self.ary_record_bat = &#x5B;]\r\n            bs = BeautifulSoup(resp.read(), &quot;html.parser&quot;)\r\n            for div in bs.findAll(&quot;div&quot;,id=&quot;registermaintbl&quot;):\r\n                th = div.find(text=&quot;\u9632&quot;)\r\n                for tr in div.findAll(&quot;tr&quot;,class_=&quot;registerStats&quot;):\r\n                    ary_1_record = &#x5B;]\r\n                    for td in tr.findAll(&quot;td&quot;):\r\n                        ary_1_record.append(td.get_text())\r\n                    if th == None :\r\n                        self.ary_record_bat.append(ary_1_record)\r\n                    else:\r\n                        self.ary_record_pitch.append(ary_1_record)\r\n            return True\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef get_player_list_detail( resp ):\r\n    ary_cPlayer = &#x5B;]\r\n    bs = BeautifulSoup(resp.read(), &quot;html.parser&quot;)\r\n    # find name\r\n    for tr in bs.findAll(&quot;tr&quot;,{&quot;class&quot;:&quot;rosterPlayer&quot;}):\r\n        td = tr.find(&quot;td&quot;,{&quot;class&quot;:&quot;rosterRegister&quot;})\r\n        a = td.find(&quot;a&quot;)\r\n        if a == None :\r\n            continue\r\n        url  = a.attrs&#x5B;&quot;href&quot;]\r\n        name = a.get_text()\r\n        cPlayer = NpbPlayer(name, url)\r\n        ary_cPlayer.append(cPlayer)\r\n    return ary_cPlayer\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef get_player_list( team_url ):\r\n    try:\r\n        resp = urllib.request.urlopen(team_url)\r\n    except urllib.error.HTTPError as e:\r\n        print(&quot;HTTP-Error : &quot;, e.code)\r\n        return None\r\n    except urllib.error.URLError as e:\r\n        print(&quot;URL-Error : &quot;, e.reason)\r\n        return None\r\n    else:\r\n        ary_cPlayer = get_player_list_detail(resp)\r\n        return ary_cPlayer\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef generate_record_file_for_1team( team_url, prefix ):\r\n    ary_cPlayer = get_player_list(team_url)\r\n    for cPlayer in ary_cPlayer:\r\n        if True == cPlayer.get_record():\r\n            print(&quot;now generating %s&quot; % cPlayer.name)\r\n            cPlayer.output_record_to_csv_file_bat(  &quot;.\/da\/data_%s_%s_da.txt&quot; % (prefix, cPlayer.name))\r\n            cPlayer.output_record_to_csv_file_pitch(&quot;.\/pt\/data_%s_%s_pt.txt&quot; % (prefix, cPlayer.name))\r\n<\/pre>\n<h1 class=\"my_h\">3. \u5099\u5fd8\u9332<\/h1>\n<p>python3\u3067\u306f reload()\u304c\u4f7f\u3048\u306a\u304f\u306a\u3063\u305f\u3002<br \/>\n\u540c\u3058\u3053\u3068\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u73fe\u3067\u304d\u308b\u3002<\/p>\n<pre>\r\nimport importlib\r\nimportlib.reload(lib_npb)\r\n<\/pre>\n<p>\u30b7\u30fc\u30ba\u30f3\u4e2d\u306f\u65e5\u3005\u6210\u7e3e\u304c\u5909\u5316\u3059\u308b\u306e\u3067\u3001\u3068\u304d\u3069\u304d\u4ee5\u4e0b\u3092\u5b9f\u884c\u3057\u3066\u6700\u65b0\u60c5\u5831\u3092\u53ce\u96c6\u3059\u308b\u3002<\/p>\n<pre>\r\nimport lib_npb\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_c.html\", \"C\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_g.html\", \"G\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_db.html\", \"DN\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_t.html\", \"T\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_s.html\", \"Y\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_d.html\", \"D\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_h.html\", \"H\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_f.html\", \"F\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_m.html\", \"M\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_l.html\", \"L\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_e.html\", \"E\")\r\nlib_npb.generate_record_file_for_1team(\"http:\/\/npb.jp\/bis\/teams\/rst_bs.html\", \"O\")\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u306e (86) NPB\u30d7\u30ed\u91ce\u7403\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u53d6\u5f97\u3059\u308b\u3002 \u3067\u306f\u3001\u4e00\u4eba\u306e\u9078\u624b\u306e\u7d39\u4ecb\u30da\u30fc\u30b8\u306eURL\u3092\u5165\u529b\u3057\u3001\u4e00\u4eba\u306e\u9078\u624b\u306e\u5e74\u5225\u6210\u7e3e\u3092\u51fa\u529b\u3059\u308b\u3060\u3051\u306e\u6a5f\u80fd\u3060\u3063\u305f\u3002\u6570\u767e\u4eba\u3082\u306e\u30d7\u30ed\u91ce\u7403\u9078\u624b\u306b\u3064\u3044\u3066\u3053\u308c\u3092\u624b\u4f5c\u696d\u3067\u7e70\u308a\u8fd4\u3059\u306e\u306f\u5927\u5909\u3060\u3002 \u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog87\/\">\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":[30,26],"tags":[],"class_list":["post-1032","post","type-post","status-publish","format-standard","hentry","category-beautifulsoup","category-web"],"views":3933,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1032","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=1032"}],"version-history":[{"count":30,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"predecessor-version":[{"id":2728,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/1032\/revisions\/2728"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}