{"id":837,"date":"2016-11-20T01:27:47","date_gmt":"2016-11-19T16:27:47","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=837"},"modified":"2018-11-07T16:55:16","modified_gmt":"2018-11-07T07:55:16","slug":"blog84","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog84\/","title":{"rendered":"(84) BeautifulSoup\u3067\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3092\u52b9\u7387\u5316"},"content":{"rendered":"<p>\u5148\u306e\u300c<a href=\"https:\/\/www.dogrow.net\/python\/?p=730\">(81) \u91ce\u7403\u9078\u624b\u306e\u6210\u7e3e\u3092\u4e3b\u6210\u5206\u5206\u6790 (\u7d9a\u7de81\/2)<\/a>\u300d\u3067\u306fNPB\u69d8\u306e\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u500b\u4eba\u6253\u6483\u6210\u7e3e\u3092\u53d6\u5f97\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u3002\u524d\u56de\u306f lxml\u3068 XPath\u3092\u4f7f\u3063\u3066\u500b\u4eba\u6210\u7e3e\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3057\u3066\u3044\u305f\u304c\u3001\u4eca\u56de\u306f\u3053\u306e\u51e6\u7406\u3092 <a href=\"https:\/\/www.crummy.com\/software\/BeautifulSoup\/\" target=_blank>BeautifulSoup<\/a> \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u884c\u3046\u3002<\/p>\n<h1 class=\"my_h\">1. BeautifulSoup\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h1>\n<pre>\r\n$ pip3 install beautifulsoup4\r\n<\/pre>\n<h1 class=\"my_h\">2. 2015\u5e74\u30bb\u30ea\u30fc\u30b0\u6253\u6483\u6210\u7e3e\u3092\u53d6\u5f97\u3057\u3066\u307f\u308b<\/h1>\n<p>\u2460 \u307e\u305a\u306f\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom urllib.request import urlopen\r\nfrom bs4 import BeautifulSoup\r\nhtml = urlopen(&quot;http:\/\/npb.jp\/bis\/2015\/stats\/bat_c.html&quot;)\r\nbs = BeautifulSoup(html.read())\r\n<\/pre>\n<p>\u2461 \u6b21\u306b\u3001\u53d6\u5f97\u3057\u305f\u60c5\u5831\u304b\u3089\u5fc5\u8981\u306a\u60c5\u5831\u3060\u3051\u3092\u62bd\u51fa\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nary_players = &#x5B;]\r\nfor tr in bs.findAll(&quot;tr&quot;,{&quot;class&quot;:&quot;ststats&quot;}):\r\n    ary_score = &#x5B;]\r\n    for td in tr.findAll(&quot;td&quot;):\r\n        ary_score.append(td.get_text())\r\n    ary_players.append(ary_score)\r\n<\/pre>\n<p>or <span class=mydp>next_siblings<\/span>\u3092\u4f7f\u3063\u3066\u9806\u4f4d\u5217\u4ee5\u964d\u306e\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u306e\u307f\u3092\u62bd\u51fa\u3057\u3066\u3082\u3088\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nary_players = &#x5B;]\r\nfor td in bs.findAll(&quot;tr&quot;,{&quot;class&quot;:&quot;ststats&quot;}):\r\n    ary_score = &#x5B;]\r\n    for td in tr.td.next_siblings:\r\n        ary_score.append(td.get_text())\r\n    ary_players.append(ary_score)\r\n<\/pre>\n<p>\u2462 \u6700\u5f8c\u306b\u7d50\u679c\u3092\u8868\u793a\u3059\u308b\u3002\u3053\u3053\u3067CSV\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u306b\u4fdd\u5b58\u3059\u308c\u3070\u3088\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfor score in ary_players:\r\n    print(score)\r\n<\/pre>\n<p>\u53d6\u5f97\u3057\u305f\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n<pre>\r\n['1', '\u5ddd\u7aef\\u3000\u614e\u543e', '(\u30e4)', '.336', '143', '632', '581', '87', '195', '34', '1', '8', '255', '57', '4', '3', '2', '2', '43', '0', '3', '72', '15', '.439', '.383']\r\n['2', '\u5c71\u7530\\u3000\u54f2\u4eba', '(\u30e4)', '.329', '143', '646', '557', '119', '183', '39', '2', '38', '340', '100', '34', '4', '0', '3', '81', '1', '5', '111', '11', '.610', '.416']\r\n['3', '\u7b52\u9999\\u3000\u5609\u667a', '(\u30c7)', '.317', '138', '568', '496', '79', '157', '28', '1', '24', '259', '93', '0', '0', '0', '2', '68', '0', '2', '98', '5', '.522', '.400']\r\n['4', '\u30eb\u30ca', '(\u4e2d)', '.292', '134', '564', '496', '61', '145', '26', '1', '8', '197', '60', '11', '0', '0', '6', '57', '2', '5', '77', '13', '.397', '.367']\r\n['5', '\u30ed\u30da\u30b9', '(\u30c7)', '.291', '140', '565', '516', '63', '150', '29', '1', '25', '256', '73', '1', '1', '0', '3', '44', '3', '2', '82', '14', '.496', '.347']\r\n['6', '\u5e73\u7530\\u3000\u826f\u4ecb', '(\u4e2d)', '.283', '130', '559', '491', '76', '139', '27', '3', '13', '211', '53', '11', '7', '1', '0', '64', '1', '3', '86', '5', '.430', '.369']\r\n['7', '\u9ce5\u8c37\\u3000\u656c', '(\u795e)', '.281', '143', '646', '551', '69', '155', '21', '4', '6', '202', '42', '9', '6', '2', '3', '89', '2', '1', '77', '8', '.367', '.380']\r\n['8', '\u798f\u7559\\u3000\u5b5d\u4ecb', '(\u795e)', '.281', '140', '569', '495', '53', '139', '24', '3', '20', '229', '76', '1', '2', '1', '7', '65', '1', '1', '75', '15', '.463', '.361']\r\n['9', '\u30de\u30fc\u30c8\u30f3', '(\u795e)', '.276', '140', '583', '544', '46', '150', '27', '0', '9', '204', '59', '0', '1', '0', '5', '31', '0', '3', '77', '21', '.375', '.316']\r\n['10', '\u68b6\u8c37\\u3000\u9686\u5e78', '(\u30c7)', '.275', '134', '578', '520', '70', '143', '35', '2', '13', '221', '66', '28', '13', '2', '2', '54', '0', '0', '132', '4', '.425', '.342']\r\n['11', '\u65b0\u4e95\\u3000\u8cb4\u6d69', '(\u5e83)', '.275', '125', '480', '426', '52', '117', '22', '2', '7', '164', '57', '3', '0', '0', '4', '48', '1', '2', '73', '15', '.385', '.348']\r\n['12', '\u7530\u4e2d\\u3000\u5e83\u8f14', '(\u5e83)', '.274', '141', '590', '543', '61', '149', '33', '9', '8', '224', '45', '6', '7', '5', '1', '34', '2', '7', '105', '8', '.413', '.325']\r\n['13', '\u30b4\u30e1\u30b9', '(\u795e)', '.271', '143', '601', '520', '49', '141', '28', '0', '17', '220', '72', '0', '1', '0', '3', '72', '1', '6', '134', '15', '.423', '.364']\r\n['14', '\u30a8\u30eb\u30ca\u30f3\u30c7\u30b9', '(\u4e2d)', '.271', '138', '548', '498', '54', '135', '27', '2', '11', '199', '58', '5', '3', '10', '4', '35', '1', '0', '106', '13', '.400', '.317']\r\n['15', '\u96c4\u5e73', '(\u30e4)', '.270', '141', '585', '551', '57', '149', '33', '4', '8', '214', '60', '7', '4', '1', '3', '27', '1', '3', '82', '7', '.388', '.307']\r\n['16', '\u5742\u672c\\u3000\u52c7\u4eba', '(\u5de8)', '.269', '130', '558', '479', '50', '129', '21', '3', '12', '192', '68', '10', '4', '9', '5', '65', '1', '0', '79', '5', '.401', '.353']\r\n['17', '\u7560\u5c71\\u3000\u548c\u6d0b', '(\u30e4)', '.268', '137', '584', '512', '64', '137', '26', '0', '26', '241', '105', '0', '0', '0', '8', '62', '0', '2', '92', '10', '.471', '.344']\r\n['18', '\u5927\u5cf6\\u3000\u6d0b\u5e73', '(\u4e2d)', '.260', '142', '620', '565', '70', '147', '20', '4', '6', '193', '27', '22', '8', '10', '1', '39', '1', '5', '65', '5', '.342', '.313']\r\n['19', '\u30d0\u30eb\u30c7\u30a3\u30ea\u30b9', '(\u30c7)', '.258', '139', '525', '465', '38', '120', '23', '0', '13', '182', '56', '0', '0', '0', '5', '43', '2', '12', '62', '12', '.391', '.333']\r\n['20', '\u83ca\u6c60\\u3000\u6dbc\u4ecb', '(\u5e83)', '.254', '143', '644', '562', '62', '143', '20', '3', '8', '193', '32', '19', '9', '49', '2', '29', '2', '2', '92', '7', '.343', '.292']\r\n['21', '\u4e0a\u672c\\u3000\u535a\u7d00', '(\u795e)', '.253', '108', '452', '375', '44', '95', '18', '1', '4', '127', '31', '19', '11', '29', '0', '44', '1', '4', '69', '1', '.339', '.338']\r\n['22', '\u9577\u91ce\\u3000\u4e45\u7fa9', '(\u5de8)', '.251', '130', '479', '434', '49', '109', '20', '3', '15', '180', '52', '3', '2', '5', '2', '34', '0', '4', '81', '12', '.415', '.310']\r\n['23', '\u4e38\\u3000\u4f73\u6d69', '(\u5e83)', '.249', '143', '633', '530', '81', '132', '28', '1', '19', '219', '63', '15', '7', '4', '4', '94', '2', '1', '143', '4', '.413', '.361']\r\n['24', '\u4e2d\u6751\\u3000\u60a0\u5e73', '(\u30e4)', '.231', '136', '502', '442', '36', '102', '14', '0', '2', '122', '33', '3', '2', '14', '2', '40', '1', '4', '80', '9', '.276', '.299']\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u306e\u300c(81) \u91ce\u7403\u9078\u624b\u306e\u6210\u7e3e\u3092\u4e3b\u6210\u5206\u5206\u6790 (\u7d9a\u7de81\/2)\u300d\u3067\u306fNPB\u69d8\u306e\u30db\u30fc\u30e0\u30da\u30fc\u30b8\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u500b\u4eba\u6253\u6483\u6210\u7e3e\u3092\u53d6\u5f97\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u3002\u524d\u56de\u306f lxml\u3068 XPath\u3092\u4f7f\u3063\u3066\u500b\u4eba\u6210\u7e3e\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u3057\u3066\u3044\u305f\u304c\u3001\u4eca\u56de\u306f\u3053\u306e\u51e6\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog84\/\">\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-837","post","type-post","status-publish","format-standard","hentry","category-beautifulsoup","category-web"],"views":3674,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/837","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=837"}],"version-history":[{"count":23,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":2015,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/837\/revisions\/2015"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}