netkeiba.com様からのサイトより、python2.7,beautifulsoupで行う、競馬情報のスクレイピングが、途中から、うまく出来ません。
# -*- coding:utf-8 -*- import urllib2 import codecs import time from bs4 import BeautifulSoup url = 'http://db.netkeiba.com/horse/2007100001/' soup = BeautifulSoup(urllib2.urlopen(url).read(), "lxml") tekisei = soup.find('div', {'class': 'db_h_rank_review'}).findAll('td') imgs = tekisei[0].findAll('img') shiba_style = imgs[1].attrs['width'] shiba_style_1 = imgs[5].attrs['width'] print shiba_style,shiba_style_1
上記のスクリプトを動かしますと、
Traceback (most recent call last): File "C:\Users\satoru\horse\horse\test_horse_hyouka.py", line 16, in <module> shiba_style_1 = imgs[5].attrs['width'] IndexError: list index out of range
shiba_style_1 = imgs[5].attrs['width']
どうも、5番目の属性はないとの返事が返ってまいりました。
当該部分のソースコードを調べてみますと、
<div class="db_h_rank_review"> <div class="categorybar_line" > <h3 class="categorybar"><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/icon_db_tekisei_02.png" width="20" height="20" alt="" />みんなの適性レビュー</h3> </div> <dl> <dd> <table summary="ヴィップスターの適性レビュー"> <tr> <th>コース適性</th> <td><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_turf_blue.png" width="26" height="17" alt="芝" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_blue.png" width="145" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_centerline.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_gray.png" width="49" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_dirt_gray.png" width="26" height="17" alt="ダート" /></td> </tr> <tr> <th>距離適性</th> <td><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_sprint_blue.png" width="26" height="17" alt="短い" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_blue.png" width="194" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_centerline.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_gray.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_styer_gray.png" width="26" height="17" alt="長い" /></td> </tr> <tr> <th>脚質</th> <td><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_nige_blue.png" width="26" height="17" alt="逃げ" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_blue.png" width="194" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_centerline.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_gray.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_oikomi_gray.png" width="26" height="17" alt="追込" /></td> </tr> <tr> <th>成長</th> <td><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_soujuku_gray.png" width="26" height="17" alt="早熟" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_gray.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_centerline.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_blue.png" width="194" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bansei_blue.png" width="26" height="17" alt="晩成" /></td> </tr> <tr> <th>重馬場</th> <td><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_tokui_blue.png" width="26" height="17" alt="得意" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_blue.png" width="97" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_centerline.png" width="1" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_bar_gray.png" width="97" height="17" /><img src="http://cimg2.db.netkeiba.com/style/netkeiba.ja/image/review_nigate_gray.png" width="26" height="17" alt="苦手" /></td> </tr>
小生のエラーの理由は、このソースコードの中にございます、この部分
</tr>
**この</tr>処理がうまくいかず、エラーが出ていると、個人的には考えております。
**
つまり、
上記のソースコードの場合、既に取れている、
<th>コース適性</th> width="145" の数値と併せて、以下、同様に <th>距離適性</th> width="194" <th>脚質</th> width="194" <th>成長</th> width="1" <th>重馬場</th> width="97"の数値を取ることが出来るように、したいのです。
</tr>処理が原因とは断定できませんが、いろいろ試しましたが、うまく出来ませんでした。先輩方の御教示、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/11 02:37