Jump to content

Please help me with this code


Recommended Posts

I have this code, and I am trying to get the results using HTML from a specific website. However, I am encountering a build error. Can anyone help me? https://www.minhngoc.net.vn/ket-qua-xo-so/mien-bac/27-10-2024.html

#include <GUIConstantsEx.au3>
#include <Date.au3>

; Biến lưu trữ kết quả 10 ngày gần nhất
Global $aResults[10][8]

; Tạo GUI
Global $hGUI = GUICreate("Kết quả xổ số miền Bắc - 10 ngày gần nhất", 600, 400)
Global $lblDate, $lblDB, $lbl1st, $lbl2nd, $lbl3rd, $lbl4th, $lbl5th, $lbl6th, $lbl7th
Global $btnPrev = GUICtrlCreateButton("Trang Trước", 150, 350, 100, 30)
Global $btnNext = GUICtrlCreateButton("Trang Sau", 350, 350, 100, 30)

; Tạo các nhãn để hiển thị kết quả
$lblDate = GUICtrlCreateLabel("", 10, 10, 580, 20)
$lblDB = GUICtrlCreateLabel("", 20, 40, 580, 20)
$lbl1st = GUICtrlCreateLabel("", 20, 70, 580, 20)
$lbl2nd = GUICtrlCreateLabel("", 20, 100, 580, 20)
$lbl3rd = GUICtrlCreateLabel("", 20, 130, 580, 20)
$lbl4th = GUICtrlCreateLabel("", 20, 160, 580, 20)
$lbl5th = GUICtrlCreateLabel("", 20, 190, 580, 20)
$lbl6th = GUICtrlCreateLabel("", 20, 220, 580, 20)
$lbl7th = GUICtrlCreateLabel("", 20, 250, 580, 20)

Local $iCurrentPage = 0 ; Trang hiện tại

; Lấy kết quả từ trang web cho 10 ngày gần nhất
For $i = 0 To 9
    Local $sDate = _DateAdd('D', -$i, _NowCalcDate())
    Local $sFormattedDate = StringReplace($sDate, "-", "/")
    $aResults[$i][0] = $sFormattedDate
    Local $sData = FetchData($sFormattedDate)
    If Not @error Then
        ParseData($sData, $i)
    Else
        MsgBox(16, "Lỗi", "Không thể lấy dữ liệu từ trang web cho ngày: " & $sFormattedDate)
    EndIf
Next

; Hàm lấy dữ liệu từ URL
Func FetchData($sDate)
    Local $sURL = "https://www.minhngoc.net.vn/ket-qua-xo-so/mien-bac/" & $sDate & ".html"
    Local $sData = InetRead($sURL)
    If @error Then
        Return SetError(1, 0, "")
    EndIf
    ; Ghi nội dung HTML ra file để kiểm tra
    FileWrite("output_" & $sDate & ".html", $sData)
    Return BinaryToString($sData)
EndFunc

; Hàm phân tích dữ liệu HTML từ trang web
Func ParseData($sData, $iDayIndex)
    Local $aMatch

    ; Đặc biệt
    $aMatch = StringRegExp($sData, '(?s)Đặc biệt.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][1] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải nhất
    $aMatch = StringRegExp($sData, '(?s)Giải nhất.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][2] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải nhì
    $aMatch = StringRegExp($sData, '(?s)Giải nhì.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][3] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải ba
    $aMatch = StringRegExp($sData, '(?s)Giải ba.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][4] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải tư
    $aMatch = StringRegExp($sData, '(?s)Giải tư.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][5] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải năm
    $aMatch = StringRegExp($sData, '(?s)Giải năm.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][6] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải sáu
    $aMatch = StringRegExp($sData, '(?s)Giải sáu.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][7] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải bảy
    $aMatch = StringRegExp($sData, '(?s)Giải bảy.*?>(.*?)<', 1)
    If Not IsArray($aMatch) Or UBound($aMatch) = 0 Then
        $aResults[$iDayIndex][8] = "Không tìm thấy dữ liệu"
    Else
        $aResults[$iDayIndex][8] = $aMatch[0]
    EndIf
EndFunc


; Hàm để cập nhật trang hiển thị kết quả
Func UpdatePage($iPage)
    GUICtrlSetData($lblDate, "Ngày: " & $aResults[$iPage][0])
    GUICtrlSetData($lblDB, "Đặc biệt: " & $aResults[$iPage][1])
    GUICtrlSetData($lbl1st, "Giải nhất: " & $aResults[$iPage][2])
    GUICtrlSetData($lbl2nd, "Giải nhì: " & $aResults[$iPage][3])
    GUICtrlSetData($lbl3rd, "Giải ba: " & $aResults[$iPage][4])
    GUICtrlSetData($lbl4th, "Giải tư: " & $aResults[$iPage][5])
    GUICtrlSetData($lbl5th, "Giải năm: " & $aResults[$iPage][6])
    GUICtrlSetData($lbl6th, "Giải sáu: " & $aResults[$iPage][7])
    GUICtrlSetData($lbl7th, "Giải bảy: " & $aResults[$iPage][8])
EndFunc

; Hiển thị trang đầu tiên
UpdatePage($iCurrentPage)
GUISetState(@SW_SHOW)

; Vòng lặp GUI
While 1
    Local $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $btnPrev
            If $iCurrentPage > 0 Then
                $iCurrentPage -= 1
                UpdatePage($iCurrentPage)
            EndIf
        Case $iMsg = $btnNext
            If $iCurrentPage < 9 Then
                $iCurrentPage += 1
                UpdatePage($iCurrentPage)
            EndIf
        Case $iMsg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

GUIDelete($hGUI)

 

Link to comment
Share on other sites

Hi @1stPK 👋 ,

no build errors with this code:

Spoiler
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

#include-once
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>

; Biến lưu trữ kết quả 10 ngày gần nhất
Global $aResults[10][8]

; Tạo GUI
Global $hGUI = GUICreate("Kết quả xổ số miền Bắc - 10 ngày gần nhất", 600, 400)
Global $lblDate, $lblDB, $lbl1st, $lbl2nd, $lbl3rd, $lbl4th, $lbl5th, $lbl6th, $lbl7th
Global $btnPrev = GUICtrlCreateButton("Trang Trước", 150, 350, 100, 30)
Global $btnNext = GUICtrlCreateButton("Trang Sau", 350, 350, 100, 30)

; Tạo các nhãn để hiển thị kết quả
$lblDate = GUICtrlCreateLabel("", 10, 10, 580, 20)
$lblDB = GUICtrlCreateLabel("", 20, 40, 580, 20)
$lbl1st = GUICtrlCreateLabel("", 20, 70, 580, 20)
$lbl2nd = GUICtrlCreateLabel("", 20, 100, 580, 20)
$lbl3rd = GUICtrlCreateLabel("", 20, 130, 580, 20)
$lbl4th = GUICtrlCreateLabel("", 20, 160, 580, 20)
$lbl5th = GUICtrlCreateLabel("", 20, 190, 580, 20)
$lbl6th = GUICtrlCreateLabel("", 20, 220, 580, 20)
$lbl7th = GUICtrlCreateLabel("", 20, 250, 580, 20)

Global $iCurrentPage = 0 ; Trang hiện tại

; Lấy kết quả từ trang web cho 10 ngày gần nhất
For $i = 0 To 9
    Global $sDate = _DateAdd('D', -$i, _NowCalcDate())
    Global $sFormattedDate = StringReplace($sDate, "-", "/")
    $aResults[$i][0] = $sFormattedDate
    Global $sData = FetchData($sFormattedDate)
    If Not @error Then
        ParseData($sData, $i)
    Else
        MsgBox(16, "Lỗi", "Không thể lấy dữ liệu từ trang web cho ngày: " & $sFormattedDate)
    EndIf
Next

; Hàm lấy dữ liệu từ URL
Func FetchData($sDate)
    Local $sURL = "https://www.minhngoc.net.vn/ket-qua-xo-so/mien-bac/" & $sDate & ".html"
    Local $sData = InetRead($sURL)
    If @error Then
        Return SetError(1, 0, "")
    EndIf
    ; Ghi nội dung HTML ra file để kiểm tra
    FileWrite("output_" & $sDate & ".html", $sData)
    Return BinaryToString($sData)
EndFunc   ;==>FetchData

; Hàm phân tích dữ liệu HTML từ trang web
Func ParseData($sData, $iDayIndex)
    Local $aMatch

    ; Đặc biệt
    $aMatch = StringRegExp($sData, '(?s)Đặc biệt.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][1] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải nhất
    $aMatch = StringRegExp($sData, '(?s)Giải nhất.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][2] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải nhì
    $aMatch = StringRegExp($sData, '(?s)Giải nhì.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][3] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải ba
    $aMatch = StringRegExp($sData, '(?s)Giải ba.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][4] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải tư
    $aMatch = StringRegExp($sData, '(?s)Giải tư.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][5] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải năm
    $aMatch = StringRegExp($sData, '(?s)Giải năm.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][6] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"

    ; Giải sáu
    $aMatch = StringRegExp($sData, '(?s)Giải sáu.*?class="numbers">(.*?)<', 1)
    $aResults[$iDayIndex][7] = IsArray($aMatch) And UBound($aMatch) > 0 ? $aMatch[0] : "Không có dữ liệu"


    ; Giải bảy
    $aMatch = StringRegExp($sData, '(?s)Giải bảy.*?>(.*?)<', 1)

    _ArrayDisplay($aResults, 1)
    _ArrayDisplay($aMatch, 2)

    If Not IsArray($aMatch) Or UBound($aMatch) = 0 Then
        $aResults[$iDayIndex][8] = "Không tìm thấy dữ liệu"
    Else
        $aResults[$iDayIndex][8] = $aMatch[0]
    EndIf
EndFunc   ;==>ParseData


; Hàm để cập nhật trang hiển thị kết quả
Func UpdatePage($iPage)
    GUICtrlSetData($lblDate, "Ngày: " & $aResults[$iPage][0])
    GUICtrlSetData($lblDB, "Đặc biệt: " & $aResults[$iPage][1])
    GUICtrlSetData($lbl1st, "Giải nhất: " & $aResults[$iPage][2])
    GUICtrlSetData($lbl2nd, "Giải nhì: " & $aResults[$iPage][3])
    GUICtrlSetData($lbl3rd, "Giải ba: " & $aResults[$iPage][4])
    GUICtrlSetData($lbl4th, "Giải tư: " & $aResults[$iPage][5])
    GUICtrlSetData($lbl5th, "Giải năm: " & $aResults[$iPage][6])
    GUICtrlSetData($lbl6th, "Giải sáu: " & $aResults[$iPage][7])
    GUICtrlSetData($lbl7th, "Giải bảy: " & $aResults[$iPage][8])
EndFunc   ;==>UpdatePage

; Hiển thị trang đầu tiên
UpdatePage($iCurrentPage)
GUISetState(@SW_SHOW)

; Vòng lặp GUI
While 1
    Global $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $btnPrev
            If $iCurrentPage > 0 Then
                $iCurrentPage -= 1
                UpdatePage($iCurrentPage)
            EndIf
        Case $iMsg = $btnNext
            If $iCurrentPage < 9 Then
                $iCurrentPage += 1
                UpdatePage($iCurrentPage)
            EndIf
        Case $iMsg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

GUIDelete($hGUI)

 

But your InetRead request does not provide any data (empty string is returned). So please check line 11 (and the error handling), also the lines 9, 51 and 52 to display the result array. Without a deeper look, I guess you have to get the source page by another method.

Search the forum (maybe by google) for "getting page source" or similar and you will find several answer or suggestions besides InetRead.
Hope this will help you 🤝 .

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...