Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.8 kB
3
Indexable
Never
from datetime import datetime, timedelta
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
current_date = datetime.now()

data_list = []

for day in range(1, 5):
    driver.get("https://www.thantai1.net/so-ket-qua")

    try:
        date_input = driver.find_element(By.XPATH, '//*[@id="end"]')
        date_input.clear()
        date_string = "{}-{}-{}".format(current_date.day, current_date.month, current_date.year)
        date_input.send_keys(date_string)

        view_button = driver.find_element(By.XPATH, '//*[@id="skq"]/form/div[3]/div/button')
        view_button.click()
        driver.implicitly_wait(1)
        page_source = driver.page_source
        soup = BeautifulSoup(page_source, 'html.parser')
        result_table = soup.find('table', {'class': 'table table-bordered table-striped table-sm'})
        if result_table:
            rows = result_table.find_all('tr')
            for row in rows:  
                columns = row.find_all('td')
                if len(columns) == 2:  
                    date = columns[0].text.strip()
                    numbers = columns[1].text.strip()
                    data_list.append([date, numbers])

    except Exception as e:
        print(f"Lỗi trong quá trình truy cập trang web cho ngày {date_string}: {str(e)}")
    current_date -= timedelta(days=1)

driver.quit()
for data in data_list:
    print(f"Ngày: {data[0]}, Kết quả: {data[1]}")