### 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 (``) in a BeautifulSoup object. ### `create_uid(race_id, horse_number)` **Description**: Creates a unique identifier using a race ID and horse number. ### `set_diff_time(rank, rap_time)` **Description**: Calculates the time difference based on rank and rap time. ### `convert_type(type_abbr)` **Description**: Converts a type abbreviation into a more descriptive format. ### `classify_length(length)` **Description**: Classifies a given length value. ### `concat(*args)` **Description**: Concatenates multiple arguments into a single string or value. ### `fmt_date(date_str)` **Description**: Formats a date string into a standard display format. ### `load_config(data_type)` **Description**: Loads configuration data based on the specified data type. ### `create_table_sql(data_type)` **Description**: Generates SQL statement for creating a table based on the data type. ### `create_index_sql(data_type)` **Description**: Generates SQL statement for creating an index based on the data type. ``` -------------------------------- ### History Configuration for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for scraping horse race history from netkeiba.com. It extracts details like race date, venue, and popularity rank from HTML tables. ```json { "file": "history.json", "source": "netkeiba.com horse result history page", "mode": "HTML", "columns": { "horse_id": "Set from entity_id", "race_date": "Formatted from URL via pre_func and post_func", "place": "Racing venue", "race_id": "From href", "popularity": "Betting popularity rank", "passage_rank": "Running positions through race", "prize": "Prize money with zero_fill" }, "primary_key": "id (computed from race_id + horse_number)" } ``` -------------------------------- ### keibascraper.load Source: https://github.com/new-village/keibascraper/blob/main/README.md Loads data from netkeiba.com based on the specified data type and entity ID. Supports 'entry', 'result', 'odds', and 'horse' data types. ```APIDOC ## `load` Function ### Description Loads data from netkeiba.com based on the specified data type and entity ID. ### Parameters #### Path Parameters - **data_type** (str) - Required - Type of data to load. Supported types are 'entry', 'result', 'odds', and 'horse'. - **entity_id** (str) - Required - Identifier for the data entity (e.g., race ID, horse ID). ### Returns - For 'entry' and 'result': Returns a list of `[{race}]` and list of `[{entry1}, {entry2}...`. - For 'odds': Returns a list of `[{odds1}, {odds2}...`. - For 'horse': Returns a list of `[{horse}]` and list of `[{result1}, {result2}...`. ### Raises - `ValueError`: If an unsupported data type is provided. - `RuntimeError`: If data loading or parsing fails. ``` -------------------------------- ### Handle JSONDecodeError for Configuration Files Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/errors.md Use this snippet to catch and report errors when loading configuration files that contain invalid JSON syntax. It specifically targets issues arising from corrupted or malformed configuration files. ```python from keibascraper.helper import load_config import json try: config = load_config('entry') except json.JSONDecodeError as e: print(f"Config JSON invalid: {e}") ``` -------------------------------- ### Load Race Entries Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md Use EntryLoader to fetch and parse race entry data for a given race ID. Requires an instance of EntryLoader initialized with the race ID. ```python from keibascraper.load import EntryLoader loader = EntryLoader("201206050810") race, entries = loader.load() ``` -------------------------------- ### load(data_type, entity_id) Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md Loads data from netkeiba.com based on the specified data type and entity ID. Supports loading entry, result, odds, and horse data. ```APIDOC ## load(data_type, entity_id) ### Description Loads data from netkeiba.com based on the specified data type and entity ID. Supports loading entry, result, odds, and horse data. ### Parameters #### Path Parameters - **data_type** (str) - Required - Type of data to load. One of: `'entry'`, `'result'`, `'odds'`, `'horse'`. - **entity_id** (str) - Required - Identifier for the data entity (e.g., race ID `'201206050810'`, horse ID `'2009102739'`). ### Returns - For `'entry'` data type: Returns tuple `(race_list, entry_list)` where `race_list` is a list containing one race dictionary, and `entry_list` is a list of entry dictionaries. - For `'result'` data type: Returns tuple `(race_list, result_list)` where `race_list` is a list containing one race dictionary, and `result_list` is a list of result dictionaries. - For `'odds'` data type: Returns a list of odds dictionaries directly. - For `'horse'` data type: Returns tuple `(horse_list, history_list)` where `horse_list` is a list containing one horse pedigree dictionary, and `history_list` is a list of race history dictionaries. ### Raises - `ValueError`: If `data_type` is not one of the supported types. - `RuntimeError`: If HTTP request fails, data parsing fails, or no valid data is found for the entity. ### Example - Loading Entry Data ```python import keibascraper race, entry = keibascraper.load("entry", "201206050810") print(f"Race: {race[0]['race_name']}") print(f"Entries: {len(entry)}") for horse in entry: print(f" {horse['horse_number']}: {horse['horse_name']}") ``` ### Example - Loading Odds Data ```python import keibascraper odds = keibascraper.load("odds", "201206050810") for item in odds: print(f"Horse {item['horse_number']}: Win={item['win']}, Show={item['show_min']}-{item['show_max']}") ``` ### Example - Loading Horse Pedigree ```python import keibascraper horse, history = keibascraper.load("horse", "2009102739") print(f"Father: {horse[0]['father_name']}") print(f"Recent races: {len(history)}") ``` ``` -------------------------------- ### concat(*args) Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/helper-module.md Concatenates multiple arguments of any type into a single string. Useful for combining various pieces of information. ```APIDOC ## concat(*args) ### Description Concatenate multiple arguments into a single string. ### Parameters #### Path Parameters - **args** (Any) - Required - Variable number of values to concatenate. ### Returns Concatenated string. ### Example ```python from keibascraper.helper import concat result = concat('東京', '芝', '2000') # Returns: '東京芝2000' result = concat('京都', 'ダート', 1600) # Returns: '京都ダート1600' ``` ``` -------------------------------- ### Default HTTP Headers for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/load-module.md These are the default HTTP headers used for all requests to netkeiba.com. They mimic a common browser to improve compatibility and avoid detection. ```python { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8', 'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8', 'Connection': 'keep-alive', } ``` -------------------------------- ### KeibaScraper Parse Module Imports Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/INDEX.md Import parsing functions and classes from the keibascraper.parse module. ```python from keibascraper.parse import parse_html, parse_json, HTMLParser, JSONParser ``` -------------------------------- ### Utility Functions Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/README.md Reference for utility functions used for formatting, data transformation, and HTML element extraction. ```APIDOC ## Formatting Functions ### `formatter` #### Description Formats data into a specific string representation. ### `time_to_seconds` #### Description Converts time values into seconds. ### `fmt_date` #### Description Formats date values. ## HTML Element Extraction Functions ### `get_url` #### Description Extracts a URL from HTML content. ### `get_title` #### Description Extracts the title from HTML content. ### `count_tr` #### Description Counts table row (``) elements in HTML. ## Data Transformation Functions ### `create_uid` #### Description Generates a unique identifier. ### `concat` #### Description Concatenates strings or data elements. ### `classify_length` #### Description Classifies data based on length. ``` -------------------------------- ### load_config(data_type) Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/helper-module.md Loads the configuration file for a specified data type. It utilizes caching to store and retrieve configurations efficiently, ensuring that the same data type always returns the same cached result. This function is essential for accessing configuration properties and column definitions. ```APIDOC ## `load_config(data_type)` ### Description Loads the configuration file for a specified data type. It utilizes caching to store and retrieve configurations efficiently, ensuring that the same data type always returns the same cached result. This function is essential for accessing configuration properties and column definitions. ### Method ```python load_config(data_type: str) ``` ### Parameters #### Path Parameters - **data_type** (str) - Required - Data type identifier (e.g., 'entry', 'race', 'result', 'odds', 'horse', 'history', 'cal'). ### Returns Configuration dictionary with 'property' and 'columns' keys. ### Raises - `FileNotFoundError`: If configuration file does not exist. - `json.JSONDecodeError`: If JSON file contains syntax errors. ### Example ```python from keibascraper.helper import load_config config = load_config('entry') print(config['property']['url']) columns = config['columns'] for col in columns: print(f"{col['col_name']}: {col['var_type']}") ``` ``` -------------------------------- ### Odds Configuration for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for scraping JRA odds data from netkeiba.com using JSON and jq selectors. It extracts win and show odds based on specified jq paths. ```jq ".data.odds[\"1\"]" ``` ```jq ".data.odds[\"2\"]" ``` ```json { "file": "odds.json", "source": "netkeiba.com JRA odds API (JSON)", "mode": "JSON with jq selectors", "columns": { "race_id": "Set from entity_id", "horse_number": "Extracted and zero-suppressed from jq", "win": "Win odds (単勝)", "show_min": "Minimum show odds (複勝)", "show_max": "Maximum show odds (複勝)" }, "primary_key": "id (computed from race_id + horse_number)" } ``` -------------------------------- ### Implement Retry Logic for Data Loading Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/errors.md This function demonstrates how to implement a retry mechanism with exponential backoff for `RuntimeError` during data loading. It retries up to a specified number of times before re-raising the exception. ```python import keibascraper import time def load_with_retry(data_type, entity_id, max_retries=3): for attempt in range(max_retries): try: return keibascraper.load(data_type, entity_id) except RuntimeError as e: if attempt < max_retries - 1: print(f"Attempt {attempt + 1} failed, retrying...") time.sleep(5 * (attempt + 1)) # Exponential backoff else: raise ``` -------------------------------- ### KeibaScraper Configuration File Schema Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md This is the general JSON schema for keibascraper configuration files. It defines the structure for properties and columns used in parsing. ```json { "property": { "entry": "string", "url": "string", "mode": "html|json", "selector": "string", "validator": "string" }, "columns": [ { "col_name": "string", "alias": "string", "selector": "string", "var_type": "text|integer|real", "reg": "regex pattern (optional)", "index": "entity_id (optional)", "pre_func": {...} (optional), "post_func": {...} (optional) } ] } ``` -------------------------------- ### Horse Configuration for Netkeiba Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/configuration.md Configuration for scraping horse pedigree data from netkeiba.com. It uses complex selectors to extract lineage information like father's and mother's IDs and names. ```json { "file": "horse.json", "source": "netkeiba.com horse pedigree page", "mode": "HTML with complex selectors", "columns": { "id": "Set from entity_id", "father_id": "Row 1, Column 1", "father_name": "Row 1, Column 1", "mother_id": "Row 17, Column 1", "mother_name": "Row 17, Column 1", "f_father_id": "Row 1, Column 2", "f_father_name": "Row 1, Column 2", "f_mother_id": "Row 9, Column 1", "f_mother_name": "Row 9, Column 1", "m_father_id": "Row 17, Column 2", "m_father_name": "Row 17, Column 2", "m_mother_id": "Row 25, Column 1", "m_mother_name": "Row 25, Column 1" }, "primary_key": "id" } ``` -------------------------------- ### Generate SQLite Table Creation Query Source: https://github.com/new-village/keibascraper/blob/main/README.md Generate an SQL query to create an SQLite table for a specified data type (e.g., 'entry'). The query is designed to create the table only if it doesn't exist and sets the first column as the primary key. ```python import keibascraper query = keibascraper.create_table_sql("entry") print(query) ``` -------------------------------- ### create_table_sql(data_type) Source: https://github.com/new-village/keibascraper/blob/main/_autodocs/api-reference/package.md Generate SQLite CREATE TABLE statement from configuration. This utility function helps in setting up database tables for storing parsed horse racing data. ```APIDOC ## create_table_sql(data_type) ### Description Generate SQLite CREATE TABLE statement from configuration. This utility function helps in setting up database tables for storing parsed horse racing data. ### Parameters #### Path Parameters - **data_type** (str) - Required - Data type: `'entry'`, `'race'`, `'result'`, `'odds'`, `'horse'`, or `'history'`. ### Returns String containing SQL CREATE TABLE statement. First column is marked as PRIMARY KEY if named 'id'. ### Raises - `SystemExit` — If data_type is None - `FileNotFoundError` — If configuration file not found - `json.JSONDecodeError` — If configuration JSON is invalid ### Example ```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() ``` ```