from selenium import webdriver from selenium.webdriver.common.by import By from…
- User
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import csv from webdriver_manager.chrome import ChromeDriverManager from concurrent.futures import ThreadPoolExecutor from threading import Lock
Khóa để bảo vệ khi cập nhật số lượng liên kết đã truy cập
lock = Lock() links_visited = 0 # Biến toàn cục đếm số link đã truy cập
def scroll_page(driver, scroll_amount=700): driver.execute_script(f"window.scrollBy(0, {scroll_amount});") time.sleep(1)
def collect_links(driver, existing_links): match_links = driver.find_elements( By.XPATH, "//a[contains(@class, 'match-container')]" ) for link in match_links: existing_links.add(link.get_attribute("href"))
def scroll_full_page(driver): match_links_set = set() previous_size = 0 retries = 5
while retries > 0: scroll_page(driver) collect_links(driver, match_links_set) if len(match_links_set) == previous_size: retries -= 1 if retries == 0: print("Không còn dữ liệu mới, dừng cuộn.") print(f"Tổng số liên kết tìm thấy: {len(match_links_set)}") break else: retries = 5 previous_size = len(match_links_set) return match_links_setdef extract_ou_data(driver): ou_data = [] try: WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CLASS_NAME, "el-table__body-wrapper")) ) table = driver.find_element(By.CLASS_NAME, "el-table__body") rows = table.find_elements(By.TAG_NAME, "tr")
for row in rows: cells = row.find_elements(By.TAG_NAME, "td") if len(cells) == 5: time_live, score, ou, over, under = [ cell.text.strip() for cell in cells ] ou_data.append( { "time_live": time_live, "score": score, "ou": ou, "over": over, "under": under, } ) elif len(cells) == 4: time_live, ou, over, under = [cell.text.strip() for cell in cells] ou_data.append( { "time_live": time_live, "score": "", "ou": ou, "over": over, "under": under, } ) except Exception as e: print(f"Có lỗi xảy ra khi trích xuất dữ liệu Tài/Xỉu: {e}") return ou_datadef convert_to_dict(ou_data): ou_dict = {} for item in ou_data: ou_dict[item["time_live"]] = item return ou_dict
def extract_asian_handicap_data(driver, ou_dict): handicap_data = [] try: WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CLASS_NAME, "el-table__body-wrapper")) ) table = driver.find_element(By.CLASS_NAME, "el-table__body") rows = table.find_elements(By.TAG_NAME, "tr")
- Assistant
- User
file_exists = os.path.isfile("match_data.csv") cái này bị lỗi
- Assistant
- User
trong cái match time text có cả ngày và giờ, liệu tôi có thể tách nó ra không, hay để lúc truy vấn thì dùng điều kiện lọc
- Assistant