### Install keibascraper Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Install the keibascraper library using pip. This is the first step to using the library. ```bash pip install keibascraper ``` -------------------------------- ### Install keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Install the keibascraper library using pip. Ensure you are using Python 3.8 or above. ```bash python -m pip install keibascraper ``` -------------------------------- ### Generate and Execute SQLite CREATE TABLE Statements Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Generate SQLite CREATE TABLE statements for various data types and execute them to set up a database. This example also shows how to insert data into the created tables. ```python import keibascraper import sqlite3 # Create database conn = sqlite3.connect('keiba.db') cursor = conn.cursor() # Create tables cursor.execute(keibascraper.create_table_sql('entry')) cursor.execute(keibascraper.create_table_sql('result')) cursor.execute(keibascraper.create_table_sql('horse')) cursor.execute(keibascraper.create_table_sql('history')) conn.commit() # Insert data race, entries = keibascraper.load('entry', '201206050810') for entry in entries: cursor.execute( """INSERT INTO entry (race_id, bracket, horse_number, horse_id, horse_name, ...) VALUES (?, ?, ?, ?, ?, ...)""", tuple(entry.values()) ) conn.commit() conn.close() ``` -------------------------------- ### Time Parsing Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Example configuration for parsing time values from HTML. It uses a pre-processing function to convert time formats (M:S.F or H:M:S.F) into total seconds. ```json { "col_name": "rap_time", "selector": "...", "pre_func": {"name": "time_to_seconds", "args": ["rap_time"]}, "var_type": "real", "reg": "(\\d+.\\d+)" } ``` -------------------------------- ### Date Parsing Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Example configuration for parsing dates from HTML. It specifies the input format (YYYYMMDD) and the desired output format (YYYY-MM-DD) using a post-processing function. ```json { "col_name": "race_date", "selector": "...", "var_type": "text", "reg": "\\d+", "post_func": {"name": "fmt_date", "args": ["race_date"]} } ``` -------------------------------- ### Load Race Odds Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Retrieve odds data for all horses in a specific race. The example demonstrates how to sort odds to find favorites and underdogs, displaying win and show odds. ```python import keibascraper # Get odds for all horses in a race odds_list = keibascraper.load('odds', '201206050810') # Find favorites and underdogs for odds in sorted(odds_list, key=lambda x: x['win']): print(f"Horse {odds['horse_number']:2d}: Win={odds['win']:5.1f} Show={odds['show_min']:.1f}-{odds['show_max']:.1f}") ``` -------------------------------- ### Prevent SystemExit by Ensuring Data Type Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/errors.md This example shows how to prevent a SystemExit by ensuring that the `data_type` variable is not None before calling `create_table_sql`. It includes a check and raises a ValueError if the condition is not met. ```python from keibascraper.helper import create_table_sql data_type = 'entry' # Ensure this is not None if not data_type: raise ValueError("data_type must be specified") sql = create_table_sql(data_type) ``` -------------------------------- ### Get Horse Pedigree and History Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Fetch a horse's pedigree information, including sire and dam details, along with its recent race history. The example shows how to access horse details and display the last five races. ```python import keibascraper # Get horse pedigree and race history horse_info, history = keibascraper.load('horse', '2009102739') horse = horse_info[0] print(f"Horse: {horse['id']}") print(f"Sire: {horse['father_name']} (id: {horse['father_id']})") print(f"Dam: {horse['mother_name']} (id: {horse['mother_id']})") # Show recent races print(f"\nRace History ({len(history)} races):") for h in history[-5:]: print(f"{h['race_date']} {h['race_name']:20} #{h['horse_number']:2d} Fin: {h['rank']} ({h['rap_time']}s)") ``` -------------------------------- ### HTML Mode CSS Selectors Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Examples of CSS selectors for targeting elements in HTML mode. Use 'null' for computed fields. ```python "selector": "td.HorseInfo a" # Select link in horse info cell "selector": "td:nth-of-type(5)" # Select 5th table cell "selector": "null" # No selector (computed value) ``` -------------------------------- ### JSON Mode jq Paths Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Examples of jq paths for selecting data from JSON structures. Supports array access and key extraction. ```python "selector": ".data.odds[\"1\"]" # Array access "selector": ".data.odds[\"1\"][][] | .[0]" # Nested array "selector": "to_entries[] | .key" # Key extraction ``` -------------------------------- ### Load Race Entry Data Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Load entry data for a specific race using its ID. This example demonstrates how to retrieve race details and the number of entries. ```python import keibascraper # Load entry data for a race race, entries = keibascraper.load('entry', '201206050810') print(f"Race: {race[0]['race_name']}") print(f"Entries: {len(entries)}") ``` -------------------------------- ### Load Odds Data Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Load betting odds for a specific race using its ID. This example shows how to access win odds for a particular horse. ```python # Load odds odds = keibascraper.load('odds', '201206050810') print(f"Win odds for horse 1: {odds[0]['win']}") ``` -------------------------------- ### Load Horse Pedigree Data Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Load horse pedigree and history data using a horse ID. This example retrieves the sire's name. ```python # Load horse pedigree horse, history = keibascraper.load('horse', '2009102739') print(f"Sire: {horse[0]['father_name']}") ``` -------------------------------- ### Handle Specific 'No Valid Data Found' RuntimeError Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/errors.md This example shows how to specifically catch a `RuntimeError` indicating that no valid data was found for a given entity ID. It prints a user-friendly message and returns None. ```python import keibascraper try: # Invalid race ID race, results = keibascraper.load('result', '000000000000') except RuntimeError as e: print(f"Error: {e}") # RuntimeError: No valid data found for result with ID 000000000000 ``` -------------------------------- ### Regex Patterns for Extraction Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Examples of regex patterns used for extracting and converting data. Supports capture groups and automatic comma removal for numeric types. ```python "reg": "\\d+" # Extract digits only "reg": "(\\d+)\\([+-]?\\d*\\)" # Extract number before parentheses "reg": "/horse/(\\w+)" # Extract ID from URL path ``` -------------------------------- ### Initialize and Parse HTML Content Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/parse-module.md Instantiate HTMLParser with configuration and HTML content, then call parse() to extract data. Ensure HTML content and configuration are valid. ```python from keibascraper.parse import HTMLParser html = "..." # HTML content parser = HTMLParser('entry', html, '201206050810') entries = parser.parse() ``` -------------------------------- ### Get All Race IDs for a Month Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Retrieves a list of all race IDs for a given year and month. This is useful for batch processing. ```python import keibascraper race_ids = keibascraper.race_list(2022, 7) ``` -------------------------------- ### Store Data in SQLite Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Demonstrates how to create an SQLite table for 'entry' data using the library's SQL generation function and then insert data. Requires an active SQLite connection. ```python import keibascraper import sqlite3 conn = sqlite3.connect('keiba.db') cursor = conn.cursor() cursor.execute(keibascraper.create_table_sql('entry')) # ... insert data ... ``` -------------------------------- ### Parse HTML with Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Parses HTML content using the 'entry' configuration. The configuration is loaded automatically, simplifying the parsing process. ```python from keibascraper.parse import parse_html # Configuration is loaded automatically data = parse_html('entry', html_content, '201206050810') ``` -------------------------------- ### Get Race IDs for a Month Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Retrieve a list of all race IDs for a given year and month. This is useful for iterating through races within a specific period. ```python import keibascraper # Get all races for July 2022 race_ids = keibascraper.race_list(2022, 7) print(f"Found {len(race_ids)} race IDs") ``` -------------------------------- ### Load Entry Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Loads the 'entry' configuration to access columns and URL templates. This is the first step in setting up data extraction. ```python from keibascraper.helper import load_config # Load entry configuration config = load_config('entry') columns = config['columns'] url_template = config['property']['url'] ``` -------------------------------- ### Extract Title from BeautifulSoup Tag Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/helper-module.md Use this function to get the 'title' attribute from a BeautifulSoup Tag. It returns None if the tag is invalid or lacks a title. ```python from bs4 import BeautifulSoup from keibascraper.helper import get_title html = 'Horse' soup = BeautifulSoup(html, 'html.parser') tag = soup.select_one('a') result = get_title(tag) # Returns: 'ゴールドシップ' ``` -------------------------------- ### Initialize and Parse JSON Content Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/parse-module.md Instantiate JSONParser with configuration and JSON text, then call parse() to extract data. The JSON text should be valid and contain the expected structure. ```python from keibascraper.parse import JSONParser json_text = "{\"data\": {\"odds\": {\"1\": {\"1\": [2.7, 1.3]}}}}" parser = JSONParser('odds', json_text, '201206050810') odds = parser.parse() ``` -------------------------------- ### Create SQLite Table from Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Generates the SQL statement for creating a table based on the 'entry' configuration. This is useful for setting up the database schema. ```python from keibascraper.helper import create_table_sql # Generate SQL for SQLite sql = create_table_sql('entry') # Output: CREATE TABLE IF NOT EXISTS entry (race_id text, bracket integer, ...); ``` -------------------------------- ### Bulk Operations with Rate Limiting Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Shows how to perform bulk operations by fetching a list of race IDs and then loading data for each, incorporating a random delay between requests to ensure politeness. ```python import time import random race_ids = keibascraper.race_list(2022, 7) for race_id in race_ids: try: race, entries = keibascraper.load('entry', race_id) except RuntimeError: pass # Additional delay for politeness time.sleep(random.uniform(3, 5)) ``` -------------------------------- ### Main Package API Functions Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/README.md This section details the primary functions available in the keibascraper package for loading data and generating SQL. ```APIDOC ## `load()` ### Description Loads horse racing data. Specifics of what data is loaded depend on the context and configuration. ### Function Signature `load()` ### Parameters None explicitly documented. ### Returns Data loaded by the scraper. The exact format and content are not detailed here. ## `race_list()` ### Description Retrieves a list of horse races. This function is likely used to fetch metadata about available races. ### Function Signature `race_list()` ### Parameters None explicitly documented. ### Returns A list of race identifiers or summaries. ## `create_table_sql()` ### Description Generates SQL statements for creating database tables. This is useful for setting up a database schema to store scraped data. ### Function Signature `create_table_sql()` ### Parameters None explicitly documented. ### Returns SQL string for table creation. ## `create_index_sql()` ### Description Generates SQL statements for creating database indexes. This helps in optimizing database query performance. ### Function Signature `create_index_sql()` ### Parameters None explicitly documented. ### Returns SQL string for index creation. ``` -------------------------------- ### Load Race Calendar IDs Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md Use CalendarLoader to fetch and parse race IDs for a given month from Yahoo Keiba schedule pages. Initialize CalendarLoader with the year and month. ```python from keibascraper.load import CalendarLoader loader = CalendarLoader(2022, 7) race_ids = loader.load() ``` -------------------------------- ### Load Entry Data with keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Load entry data for a specific race using the `load` function. Requires importing the library. ```python import keibascraper race, entry = keibascraper.load("entry", "201206050810") print(race) print(entry) ``` -------------------------------- ### Get Race Results Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Load and process result data for a given race ID. Iterate through the results to display each horse's rank, name, race time, jockey, and prize money. ```python import keibascraper # Load result data race_info, results = keibascraper.load('result', '201206050810') # Process results race = race_info[0] for result in results: print(f"{result['rank']} - {result['horse_name']:15} {result['rap_time']:6.1f}s ({result['jockey_name']}) prize: {result['prize']}") ``` -------------------------------- ### Load Configuration Data Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/helper-module.md Loads configuration for a specified data type using caching. Ensure the configuration file exists and is valid JSON. ```python from keibascraper.helper import load_config config = load_config('entry') print(config['property']['url']) # Prints: https://race.netkeiba.com/race/shutuba.html?race_id={ID} columns = config['columns'] for col in columns: print(f"{col['col_name']}: {col['var_type']}") ``` -------------------------------- ### CalendarLoader Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md Load race calendar data from Yahoo Keiba schedule pages. ```APIDOC ## Class `CalendarLoader` ### Description Load race calendar data from Yahoo Keiba schedule pages. ### Methods #### `load(self) -> list` Fetch and parse race IDs for a given month. ### Returns List of expanded race IDs in format `YYYYMMDDNN`. ### Behavior - Fetches Yahoo Keiba schedule for given year/month. - Expands 8-digit date codes to 12 race numbers (01-12). - Includes error handling for malformed entries. ### Example ```python from keibascraper.load import CalendarLoader loader = CalendarLoader(2022, 7) race_ids = loader.load() ``` ``` -------------------------------- ### Load Data from Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/README.md Loads data from netkeiba.com using the specified data type and entity ID. Supported data types include 'entry', 'result', 'odds', and 'horse'. ```python keibascraper.load(data_type, entity_id) ``` -------------------------------- ### Load Race Data with Error Handling Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Demonstrates how to load race and entry data using the keibascraper library and handle potential ValueError or RuntimeError exceptions. ```python import keibascraper try: race, entries = keibascraper.load('entry', '201206050810') except ValueError as e: print(f"Invalid arguments: {e}") except RuntimeError as e: print(f"Load/parse failed: {e}") ``` -------------------------------- ### Pre-function Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for pre-processing functions, applied before regex extraction. Functions like 'get_url' and 'time_to_seconds' are available. ```json "pre_func": { "name": "function_name", "args": ["column_key1", "column_key2"] } ``` -------------------------------- ### Load Result Data with keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Load result data for a specific race using the `load` function. Requires importing the library. ```python import keibascraper race, entry = keibascraper.load("result", "201206050810") print(race) print(entry) ``` -------------------------------- ### Post-function Configuration Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for post-processing functions, applied after regex extraction. Functions like 'fmt_date' and 'concat' are available. ```json "post_func": { "name": "function_name", "args": ["column_key1", "column_key2"] } ``` -------------------------------- ### Entry Configuration for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for scraping race entry data from netkeiba.com. It defines key columns like race ID, horse number, and betting information, extracted from HTML. ```json { "file": "entry.json", "source": "netkeiba.com race entry page", "mode": "HTML", "columns": { "race_id": "Set from entity_id", "bracket": "Gate number (extracted from class)", "horse_number": "Racing number", "horse_id": "From href using regex", "horse_name": "From link title", "burden": "Weight (decimal)", "weight": "Horse weight", "weight_diff": "Weight change from last race" }, "primary_key": "id (computed from race_id + horse_number)" } ``` -------------------------------- ### Load Odds Data with keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Load odds data for a specific race using the `load` function. Requires importing the library. ```python import keibascraper odds = keibascraper.load("odds", "201206050810") print(odds) ``` -------------------------------- ### Load Horse Data with keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Load horse data, including pedigree and race history, using the `load` function with the 'horse' data type. Requires importing the library. ```python import keibascraper horse, result = keibascraper.load("horse", "2009102739") print(horse) print(result) ``` -------------------------------- ### KeibaScraper Helper Module Imports Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Import various utility functions from the keibascraper.helper module. ```python from keibascraper.helper import ( formatter, time_to_seconds, fmt_date, zero_suppress, zero_fill, get_url, get_title, count_tr, create_uid, set_diff_time, concat, convert_type, classify_length, load_config, create_table_sql, create_index_sql ) ``` -------------------------------- ### EntryLoader Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md Load entry (出走) data and race information from a specific race. ```APIDOC ## Class `EntryLoader` ### Description Load entry (出走) data and race information from a specific race. ### Methods #### `load(self) -> tuple` Fetch and parse entry data for a race. ### Returns Tuple `(race_list, entry_list)`. ### Example ```python from keibascraper.load import EntryLoader loader = EntryLoader("201206050810") race, entries = loader.load() ``` ``` -------------------------------- ### Bulk Data Loading with keibascraper Source: https://github.com/new-village/keibascraper/blob/main/README.md Retrieve a list of race IDs for a given year and month using `race_list`, then iterate to load entry data for each race. ```python import keibascraper # Get list of race IDs for July 2022 race_ids = keibascraper.race_list(2022, 7) # Loop through race IDs and load entry data for race_id in race_ids: race_info, entry_list = keibascraper.load("entry", race_id) # Process the data as needed ``` -------------------------------- ### KeibaScraper Main Package Imports Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Import core functionalities from the main keibascraper package. ```python from keibascraper import load, race_list, create_table_sql, create_index_sql ``` -------------------------------- ### create_index_sql Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Generates SQL CREATE INDEX statements for performance optimization based on the provided data type. ```APIDOC ## create_index_sql(data_type) ### Description Generates SQL CREATE INDEX statements for performance optimization. ### Signature ```python def create_index_sql(data_type: str) -> str ``` ### Parameters #### Path Parameters - **data_type** (str) - Required - Data type: `'entry'`, `'result'`, `'history'`, etc. ### Returns String containing one or more SQL CREATE INDEX statements (separated by semicolons). ### Raises - `ValueError` — If data_type is not recognized ### Indexes Created | Data Type | |-----------| | entry | race_id, horse_id | | result | race_id, horse_id | | history | race_id, horse_id | | odds | (none) | | horse | (none) | ### Example ```python import keibascraper import sqlite3 conn = sqlite3.connect('keiba.db') cursor = conn.cursor() # Create indexes for faster queries index_sql = keibascraper.create_index_sql('entry') for statement in index_sql.split(';'): if statement.strip(): cursor.execute(statement) conn.commit() # Queries by race_id or horse_id now use indexes cursor.execute('SELECT * FROM entry WHERE race_id = ?', ('201206050810',)) ``` ``` -------------------------------- ### Update KeibaScraper Library Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/errors.md Ensure you are using the latest version of the keibascraper library to benefit from bug fixes and improvements. Run this command in your terminal. ```bash pip install --upgrade keibascraper ``` -------------------------------- ### Data Loading Module Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/README.md Documentation for the data loading module, including HTTP session management and various loader classes. ```APIDOC ## HTTP Session Management ### Description Manages HTTP sessions for making requests to netkeiba.com. This likely includes handling cookies, headers, and connection pooling. ## Loader Classes ### `EntryLoader` #### Description Handles loading of entry data for races. ### `ResultLoader` #### Description Handles loading of result data for races. ### `OddsLoader` #### Description Handles loading of odds data for races. ### `HorseLoader` #### Description Handles loading of horse-specific data. ### `CalendarLoader` #### Description Handles loading of calendar or schedule data. ``` -------------------------------- ### Result Configuration for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for scraping race result data from netkeiba.com. It specifies columns such as finishing rank, horse ID, race time, and prize money, extracted from HTML. ```json { "file": "result.json", "source": "netkeiba.com database result page", "mode": "HTML", "columns": { "rank": "Finishing position", "horse_id": "From href", "rap_time": "Converted to seconds via pre_func", "diff_time": "Computed from first place time", "last_3f": "Last 600 meters time", "prize": "Prize money with zero_fill" }, "primary_key": "id (computed from race_id + horse_number)" } ``` -------------------------------- ### KeibaScraper Load Module Imports Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Import specific loading classes and functions from the keibascraper.load module. ```python from keibascraper.load import load, race_list, EntryLoader, ResultLoader, OddsLoader, HorseLoader ``` -------------------------------- ### KeibaScraper Configuration Schema Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/parse-module.md Defines the structure for JSON configuration files used to specify parsing rules, including URL, selectors, and column definitions with their types and transformations. ```json { "property": { "url": "https://...", "selector": "CSS or jq selector", "validator": "CSS selector to check data presence" }, "columns": [ { "col_name": "database_column_name", "alias": "Japanese name", "selector": "CSS selector (HTML) or jq path (JSON)", "var_type": "text|integer|real", "reg": "regex pattern for extraction", "index": "entity_id (optional)", "pre_func": {"name": "function_name", "args": ["arg1", "arg2"]}, "post_func": {"name": "function_name", "args": ["arg1", "arg2"]} } ] } ``` -------------------------------- ### Helper Module Functions Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/MANIFEST.txt This section lists and describes the public helper functions available for use. ```APIDOC ## Helper Module Functions This section lists and describes the public helper functions available for use. ### `formatter(pattern, target, var_type)` **Description**: Formats a given target value according to a specified pattern and variable type. ### `time_to_seconds(time_str)` **Description**: Converts a time string (e.g., 'MM:SS') into the total number of seconds. ### `zero_suppress(value)` **Description**: Suppresses leading zeros from a value. ### `zero_fill(value)` **Description**: Fills a value with leading zeros to a standard length. ### `get_title(soup)` **Description**: Extracts the title from a BeautifulSoup object. ### `get_url(soup)` **Description**: Extracts a URL from a BeautifulSoup object. ### `count_tr(soup)` **Description**: Counts the number of table rows (`