beautifulsoupを利用してnetkeibaからデータを取得してデータフレーム化してみる

2020年12月21日

beautifulsoupを利用してnetkeibaからデータを取得してデータフレーム化してみる

データ分析の技術をよく競馬に生かしていらっしゃる方々がたくさんいるのかnetkeibaからスクレイピングしてくるサイトをよく見かけます。私もスクレイピングでnetkeibaからデータを取得してみようと思います。
参考:Pythonで競馬サイトWebスクレイピング

コード


#%%
from bs4 import BeautifulSoup
import requests
import pandas as pd

request = requests.get('https://race.netkeiba.com/race/result.html?race_id=202010020601')

soup = BeautifulSoup(request.content, 'html.parser')

horses = soup.find_all(class_='HorseList')

horse_datas = []

for horse in horses:
    horse_data = {}
    horse_data['rank']          = horse.find(class_='Rank').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['horse_number']  = horse.find(class_='Txt_C').find('div').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['horse_name']    = horse.find(class_='Horse_Name').find('a').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['horse_detail']  = horse.find(class_='Detail_Left').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['jockey_weight'] = horse.find(class_='JockeyWeight').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['jockey_name']   = horse.find(class_='Jockey').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['race_time']     = horse.find(class_='RaceTime').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['popularity']    = horse.find(class_='OddsPeople').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['odds']          = horse.find(class_='Txt_R').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['passage']       = horse.find(class_='PassageRate').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['trainer']       = horse.find(class_='Trainer').find('a').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_data['weight']        = horse.find(class_='Weight').get_text().replace('\n','').replace(' ','').replace(' ','')
    horse_datas.append(horse_data)
df = pd.DataFrame(horse_datas)
# print(df)
#   rank horse_number horse_name horse_detail jockey_weight jockey_name race_time popularity   odds  passage trainer    weight
# 0    1            3      テンバガー           牡2          54.0          川田    2:02.8          2    2.3  2-2-2-1      藤岡  506(-10)
# 1    2            1  トーセンインパルス           牡2          54.0          浜中    2:02.9          1    1.8  6-6-6-6      角居   474(-6)
# 2    3            4       スーゼル           牝2          54.0           幸    2:03.4          7   50.5  4-4-3-3     長谷川    452(0)
# 3    4            6   ノボリスターリー           牡2          53.0         ☆富田    2:03.9          9  126.2  8-7-7-7      平田   526(+2)
# 4    5            2   ヒミノフラッシュ           牡2          54.0         鮫島駿    2:03.9          5   38.0  1-1-1-2      川村    520(0)
# 5    6            9     カオリナイト           牝2          54.0          小牧    2:04.0          8  112.9  8-9-7-7      牧田   490(-6)
# 6    7            5   パラダイスアレイ           牡2          54.0         和田竜    2:04.1          4   16.8  7-7-3-3      吉村   450(-2)
# 7    8            8    トーホウスザク           牡2          54.0          酒井    2:04.1          6   40.3  3-3-3-3       谷    478(0)
# 8    9            7   ショレアドルチェ           牝2          54.0          松山    2:04.5          3   14.2  5-5-7-7      寺島   438(-4)

しっかりとれているのが確認できました。

GETのパラメータに「race_id」がありますが、参考サイトによると「race_id=開催年+競馬場コード+開催回数+日数+レース数」となるそうです。これを回せば全レースが取得できそうです。

結構前に書いた記事ですがこちらの記事のロジックを使用して新着のみのデータも取得できるようにしたいと思います。

YouTube

2020年12月21日