Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
1
Indexable
Never
import requests
from bs4 import BeautifulSoup
import re

def get_info_by_id(ID):
    try:
        if ID < 10000000:
          ID = '0' + str(ID)
        url = f"https://diemthi.vnexpress.net/index/detail/sbd/{ID}/year/2023"
        response = requests.get(url)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, "html.parser")

        info_detail = str(soup.find("p", class_="o-detail-thisinh__cumthi"))
        ma_vung_thi = re.findall(r'\s+(\d+.+)(</p>)', info_detail)[0][0]

        diem_elements = soup.findAll("div", class_="o-detail-thisinh__diemthi")
        diem_element = diem_elements[0]
        diem = diem_element.text.strip()
        diem = re.sub(r'\s+', ' ', diem)
        pattern = r'(\w+\s\w+)\s(\d+\.\d+)'
        matches = reg.findall(pattern, diem)
        diem_dict = {subject: float(score) for subject, score in matches}
        # return f'Mã Vùng Thi: {ma_vung_thi}\n sbd:{ID}\n{diem}'
        return {'Mã Vùng Thi': ma_vung_thi,
                'sbd': ID,
                'Điểm':diem_dict
                }
    except requests.exceptions.RequestException as e:
        return f"Lỗi khi truy cập: {e}"
    except Exception as e:
        return f"Lỗi không xác định: {e}"