Bet365 AJAX接口返回编码值随机切换,如何指定获取目标编码值?
Hey there, let's break down your problem with Bet365's shifting OD field encodings and what you can do about it.
Why the OD Encoding Changes First
Bet365 uses dynamic, rotating encodings for the OD field as a core part of their anti-scraping strategy. These encoded strings are generated server-side on a per-request or timed basis specifically to make automated parsing harder. The bad news? There’s no documented way to force the server to return a specific fixed encoding via request headers, parameters, or any other method—this is intentional on their end to block scrapers from easily mapping codes to real odds.
What You Can Do Instead
Since you can’t control the encoding, focus on mapping the rotating codes to actual odds in real-time:
1. Build a Dynamic Lookup Table
You’ve already noticed that the same odds always map to the same encoding (even as the codes rotate). Use this to your advantage:
- Use Selenium to load the target page and extract the human-readable odds (like 2.4) alongside their associated selection IDs.
- Simultaneously make your API request to get the encoded OD values for those same selections.
- Pair each OD code with its corresponding visible odds and store this mapping (e.g., in a Python dictionary).
- Refresh this mapping periodically (since the codes rotate every few seconds) to keep it accurate.
Here’s a simplified example of how to implement this:
from selenium import webdriver import requests from selenium.webdriver.common.by import By # Initialize Selenium driver (ensure ChromeDriver is set up correctly) driver = webdriver.Chrome() target_page = "https://www.bet365.com/#/AC/B1/C1/D8/E83884115/F3/G0/H0/I1/P^13/Q^43316955" driver.get(target_page) # Wait for page content to load (adjust wait time as needed) driver.implicitly_wait(10) # Extract visible odds and selection IDs from the page odds_elements = driver.find_elements(By.CSS_SELECTOR, ".gl-ParticipantOddsOnly_Odds") selection_elements = driver.find_elements(By.CSS_SELECTOR, ".gl-Participant") # Get cookies from Selenium to use in API request cookies = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()} # Define your API request details api_url = "https://www.bet365.com/SportsBook.API/web?lid=1&zid=1&pd=%23AC%23B1%23C1%23D8%23E83884115%23F3%23G0%23H0%23I1%23P%5E13%23Q%5E43316955&cid=197&cgid=2" headers = { 'Host': 'www.bet365.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36', 'DNT': '1', 'Accept': '*/*', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'cors', 'Referer': 'https://www.bet365.com/', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8', } # Make the API request response = requests.get(api_url, headers=headers, cookies=cookies) # Build the odds mapping odds_mapping = {} for line in response.text.split('\n'): if 'OD=' in line and 'ID=' in line: # Parse API line to get selection ID and OD code parts = line.split(';') selection_id = next(p.split('=')[1] for p in parts if p.startswith('ID=')) od_code = next(p.split('=')[1] for p in parts if p.startswith('OD=')) # Match selection ID to visible odds (adjust attribute matching as needed) for idx, elem in enumerate(selection_elements): if selection_id in elem.get_attribute('data-id'): # Check page element for matching ID actual_odds = odds_elements[idx].text odds_mapping[od_code] = actual_odds break print("Current odds mapping:", odds_mapping) driver.quit()
2. Stick with Selenium for Direct Parsing
If the API approach feels too fragile, you can skip the API entirely and use Selenium to extract odds directly from the rendered page. While it’s slower than raw HTTP requests, it bypasses the encoding issue completely—you’re reading the same data that human users see.
3. Keep Anti-Scraping Measures in Mind
Bet365’s systems are designed to detect automated tools. To avoid getting blocked:
- Add random delays between requests to mimic human behavior.
- Rotate user agents regularly.
- Avoid making an excessive number of requests in a short time frame.
Important Legal Note
Always review Bet365’s Terms of Service before proceeding. Unauthorized scraping of their platform may result in account bans or legal consequences, depending on your location.
内容的提问来源于stack exchange,提问作者Drogen24

