Learn how to parse and extract data from Bet365 API responses
Bet365 uses a custom message format for API responses. This guide shows you how to parse these responses and extract meaningful data using the SDK’s built-in parsing utilities.
from bet365.message_parser import get_parsers# Get response from APIresponse = session.protected_get(url, params={...})# Create parsers from responseparsers = get_parsers(response.text)# Iterate through each parserfor parser in parsers: # Work with individual message segments process_parser(parser)
get_parsers() splits the response into multiple parser instances, each representing a logical message segment.
Search for specific section types with optional filters:
# Basic usage - find all CL (classification) sectionsfor idx, section in parser.find_sections("CL"): print(section.get_property("NA")) # Get name property# With property filtersfor idx, section in parser.find_sections( "CL", NA="Soccer", # Filter by name include_part_index=True): print(f"Found at index {idx}: {section.get_property('PD')}")# With custom filter functionsdef not_null(key, value): return value is not Nonefor idx, section in parser.find_sections( "CL", PD=not_null, # PD must not be null NA=not_null, # NA must not be null include_part_index=True): sport_name = section.get_property("NA") path_descriptor = section.get_property("PD") print(f"{sport_name}: {path_descriptor}")
Set include_part_index=True to get both the index and the section object. This is useful for reading tables.
Locate the content sections containing match data:
for idx, section in parser.find_sections( "CL", PV=lambda k, v: v.startswith("podcontentcontentapi"), include_part_index=True): # Found a content pod process_content_pod(parser, idx)
2
Find Match Groups
Within each content section, find match groups:
for idx, section in parser.find_sections("MG", include_part_index=True): # Read the match table table = read_table(parser, idx)
from bet365.message_parser import pretty_print_table, read_tablefor idx, section in parser.find_sections("MG", include_part_index=True): table = read_table(parser, idx) # Print formatted table to console pretty_print_table(table) # Output example: # +------------------------+-------+-------+-------+ # | Match | 1 | X | 2 | # +------------------------+-------+-------+-------+ # | Team A vs Team B | 1.50 | 3.20 | 6.50 | # | Team C vs Team D | 2.10 | 3.10 | 3.40 | # +------------------------+-------+-------+-------+
from bet365.message_parser import get_parsers, read_table, pretty_print_table, fix_data# Fetch sport homepageresponse = session.get_sport_homepage(soccer)# Parse the responsematch_tables = []for parser in get_parsers(response.text): # Find content pods for _, _ in parser.find_sections( "CL", PV=lambda k, v: v.startswith("podcontentcontentapi"), include_part_index=True ): # Find match groups within the pod for idx, _ in parser.find_sections("MG", include_part_index=True): # Read and process the table table = read_table(parser, idx) pretty_print_table(table) match_tables.append(fix_data(table))print(f"Extracted {len(match_tables)} match tables")