質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Q&A

解決済

1回答

1897閲覧

python2.7,beautifulsoupでの競馬情報のスクレイピングが、うまく出来ません。

akakage13

総合スコア89

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

0グッド

0クリップ

投稿2017/06/24 09:05

netkeiba.com様から利用させて頂いております、python2.7,beautifulsoupでの競馬情報のスクレイピングが、うまく出来ません。

# -*- coding:utf-8 -*- import urllib2 import codecs import time import random from bs4 import BeautifulSoup url='http://db.netkeiba.com/?pid=bms_leading&year=2017' soup = BeautifulSoup(urllib2.urlopen(url).read(),"lxml") tr_arr1 = soup.select("table.nk_tb_common race_table_01") print tr_arr1

上記のソースコードでは、何も吐き出しません。 soup.select の根拠となった、当サイトのソースコードが下記の通りでございます。

</ul> </div><!-- //class="db_sub_menu" --> <table cellpadding="0" cellspacing="1" summary="リーディング順位" class="nk_tb_common race_table_01"> <tr align="center"> <th rowspan="2" class="hml" nowrap>順位</th> <th rowspan="2" class="hml" nowrap>馬名</th> <th rowspan="2" class="hml" nowrap>出走<br />頭数</th> <th rowspan="2" class="hml" nowrap>勝馬<br />頭数</th> <th rowspan="2" class="hml" nowrap>出走<br />回数</th> <th rowspan="2" class="hml" nowrap>勝利<br />回数</th> <th colspan="2" class="hml" nowrap>重賞</th> <th colspan="2" class="hml" nowrap>特別</th> <th colspan="2" class="hml" nowrap>平場</th> <th colspan="2" class="hml" nowrap>芝</th> <th colspan="2" class="hml" nowrap>ダート</th> <th rowspan="2" class="hml" nowrap>勝馬<br />率</th> <th rowspan="2" class="hml" nowrap>EI</th> <th rowspan="2" class="hml" nowrap>入着賞金<br />(万円)</th> <th rowspan="2" class="hml" nowrap>平均距離<br />(芝)</th> <th rowspan="2" class="hml" nowrap>平均距離<br />(ダ)</th> <th rowspan="2" class="hml" nowrap>代表馬</th> <th rowspan="2" class="hml" nowrap>順位変動</th> </tr> <tr align="center"> <th class="hml" nowrap>出走</th> <th class="hml" nowrap>勝利</th> <th class="hml" nowrap>出走</th> <th class="hml" nowrap>勝利</th> <th class="hml" nowrap>出走</th> <th class="hml" nowrap>勝利</th> <th class="hml" nowrap>出走</th> <th class="hml" nowrap>勝利</th> <th class="hml" nowrap>出走</th> <th class="hml" nowrap>勝利</th> </tr> <tr> <td nowrap>1</td> <td class="txt_l" nowrap><a href="/horse/sire/000a00033a/">サンデーサイレンス</a></td> <td nowrap>517</td> <td nowrap>104</td>

実際の全てのソースコードを御覧いただければ、もっと良いと思いますが、

tr_arr1 = soup.select("table.nk_tb_common race_table_01") の部分は、かなり自信がございましたのに

何も吐き出さないので、苦慮しております。

先輩方の、御教示、よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

実際に試してはいませんが、書き方的にはこうではないでしょうか?
tr_arr1 = soup.select("table.nk_tb_common.race_table_01")

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
ここにselectの使い方がたくさんのっています。

投稿2017/06/25 11:45

編集2017/06/25 11:51
kurosuke___

総合スコア217

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

akakage13

2017/06/25 17:40

E3KUROSUKE様、うまく動きました。 御教示ありがとうございました。 今後ともよろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問