Results 1 to 15 of 15

Thread: [RESOLVED] get value from web page

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Resolved [RESOLVED] get value from web page

    http://www.lottoconsult.it/estrazion....asp?anno=2025

    Bsed this link how to get all value from table?
    Tks.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    501

    Re: get value from web page

    You need to reference Microsoft HTML Object Library & Microsoft XML, 3.0

    You didn't mention what value you wanted to grab; this just loops the known HTML of the web page and fills a MSFlexGrid control.

    Code:
    Private Sub Command1_Click()
        
        Dim HTTPRequest As MSXML2.XMLHTTP
        Dim url As String
        Dim htmlDoc As HTMLDocument
    
        Dim tbl As HTMLTable
        Dim tblRow As HTMLTableRow
        Dim tblCol As HTMLTableCol
        
        Dim rowNum As Integer
        Dim colNum As Integer
        Dim innerColNum As Integer
        
        Dim grdDataColValue As String
        
        Set HTTPRequest = New MSXML2.XMLHTTP
        Set htmlDoc = New HTMLDocument
        
        ' Open the HTTP request and send it
        url = "http://www.lottoconsult.it/estrazioni_lotto.asp?anno=2025"
        
        HTTPRequest.Open "GET", url, False
        HTTPRequest.send
    
        htmlDoc.body.innerHTML = HTTPRequest.responseText
        
        ' extract the table
        innerColNum = 1
        Set tbl = htmlDoc.getElementsByTagName("table")(7) ' examine the page source to see which table holds the data to get as it doesn't have an id
        If Not tbl Is Nothing Then
            
            ' find all the table rows
            For Each tblRow In tbl.getElementsByTagName("tr")
        
                If rowNum = 0 Then
                    ' count the columns needed for the grid - first header row
          
                    ' this site doesn't use "th" for the table header
                    ' count the column headers
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        colNum = colNum + 1
                    Next tblCol
               
                    ' set the number of columns
                    grdData.cols = colNum
                    
                    ' write the column headers
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        grdData.TextMatrix(0, colNum) = tblCol.innerText
                        grdData.ColWidth(colNum) = 1250
                        colNum = colNum + 1
                    Next tblCol
    
                ElseIf rowNum = 1 Then
                    ' second header row
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        If colNum < 2 Then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grdDataColValue = grdDataColValue & tblCol.children(0).innerText & " "
                            colNum = colNum + 1
                        Else
                            If colNum = 2 Then
                                ' write the first 2 cols to col 1 of the grid
                                grdData.TextMatrix(rowNum, 0) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                            grdDataColValue = grdDataColValue & tblCol.innerText & " "
                            innerColNum = innerColNum + 1
                            
                            If innerColNum = 6 Then
                                ' once the 5 numbers are found for the column, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grdData.TextMatrix(rowNum, colNum - 2) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                        End If
                        
                    Next tblCol
                Else
                    ' add the row of data to the grid
                    grdData.rows = grdData.rows + 1
                   
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        If colNum < 2 Then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grdDataColValue = grdDataColValue & tblCol.children(0).innerText & " "
                            colNum = colNum + 1
                        Else
                            If colNum = 2 Then
                                ' write the first 2 cols to col 1 of the grid
                                grdData.TextMatrix(rowNum, 0) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                        
                            ' all other rows have 5 numbers per column
                            grdDataColValue = grdDataColValue & tblCol.innerText & " "
                            innerColNum = innerColNum + 1
                            
                            If innerColNum = 6 Then
                                ' once the 5 numbers are found, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grdData.TextMatrix(rowNum, colNum - 2) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                        End If
                        
                    Next tblCol
    
                End If
                
                rowNum = rowNum + 1
                
                DoEvents
            Next tblRow
        
        End If
            
        ' release the objects
        Set HTTPRequest = Nothing
        Set htmlDoc = Nothing
        
        lblStatus.Caption = "Extracted " & CStr(grdData.rows) & " rows from table": DoEvents
    End Sub
    Attached Images Attached Images  

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Re: get value from web page

    Quote Originally Posted by jdelano View Post
    you need to reference microsoft html object library & microsoft xml, 3.0

    you didn't mention what value you wanted to grab; this just loops the known html of the web page and fills a msflexgrid control.

    Code:
    private sub command1_click()
        
        dim httprequest as msxml2.xmlhttp
        dim url as string
        dim htmldoc as htmldocument
    
        dim tbl as htmltable
        dim tblrow as htmltablerow
        dim tblcol as htmltablecol
        
        dim rownum as integer
        dim colnum as integer
        dim innercolnum as integer
        
        dim grddatacolvalue as string
        
        set httprequest = new msxml2.xmlhttp
        set htmldoc = new htmldocument
        
        ' open the http request and send it
        url = "http://www.lottoconsult.it/estrazioni_lotto.asp?anno=2025"
        
        httprequest.open "get", url, false
        httprequest.send
    
        htmldoc.body.innerhtml = httprequest.responsetext
        
        ' extract the table
        innercolnum = 1
        set tbl = htmldoc.getelementsbytagname("table")(7) ' examine the page source to see which table holds the data to get as it doesn't have an id
        if not tbl is nothing then
            
            ' find all the table rows
            for each tblrow in tbl.getelementsbytagname("tr")
        
                if rownum = 0 then
                    ' count the columns needed for the grid - first header row
          
                    ' this site doesn't use "th" for the table header
                    ' count the column headers
                    colnum = 0
                    for each tblcol in tblrow.getelementsbytagname("td")
                        colnum = colnum + 1
                    next tblcol
               
                    ' set the number of columns
                    grddata.cols = colnum
                    
                    ' write the column headers
                    colnum = 0
                    for each tblcol in tblrow.getelementsbytagname("td")
                        grddata.textmatrix(0, colnum) = tblcol.innertext
                        grddata.colwidth(colnum) = 1250
                        colnum = colnum + 1
                    next tblcol
    
                elseif rownum = 1 then
                    ' second header row
                    colnum = 0
                    for each tblcol in tblrow.getelementsbytagname("td")
                        if colnum < 2 then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grddatacolvalue = grddatacolvalue & tblcol.children(0).innertext & " "
                            colnum = colnum + 1
                        else
                            if colnum = 2 then
                                ' write the first 2 cols to col 1 of the grid
                                grddata.textmatrix(rownum, 0) = grddatacolvalue
                                grddatacolvalue = ""
                                innercolnum = 1
                                colnum = colnum + 1
                            end if
                            
                            grddatacolvalue = grddatacolvalue & tblcol.innertext & " "
                            innercolnum = innercolnum + 1
                            
                            if innercolnum = 6 then
                                ' once the 5 numbers are found for the column, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grddata.textmatrix(rownum, colnum - 2) = grddatacolvalue
                                grddatacolvalue = ""
                                innercolnum = 1
                                colnum = colnum + 1
                            end if
                            
                        end if
                        
                    next tblcol
                else
                    ' add the row of data to the grid
                    grddata.rows = grddata.rows + 1
                   
                    colnum = 0
                    for each tblcol in tblrow.getelementsbytagname("td")
                        if colnum < 2 then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grddatacolvalue = grddatacolvalue & tblcol.children(0).innertext & " "
                            colnum = colnum + 1
                        else
                            if colnum = 2 then
                                ' write the first 2 cols to col 1 of the grid
                                grddata.textmatrix(rownum, 0) = grddatacolvalue
                                grddatacolvalue = ""
                                innercolnum = 1
                                colnum = colnum + 1
                            end if
                        
                            ' all other rows have 5 numbers per column
                            grddatacolvalue = grddatacolvalue & tblcol.innertext & " "
                            innercolnum = innercolnum + 1
                            
                            if innercolnum = 6 then
                                ' once the 5 numbers are found, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grddata.textmatrix(rownum, colnum - 2) = grddatacolvalue
                                grddatacolvalue = ""
                                innercolnum = 1
                                colnum = colnum + 1
                            end if
                            
                        end if
                        
                    next tblcol
    
                end if
                
                rownum = rownum + 1
                
                doevents
            next tblrow
        
        end if
            
        ' release the objects
        set httprequest = nothing
        set htmldoc = nothing
        
        lblstatus.caption = "extracted " & cstr(grddata.rows) & " rows from table": Doevents
    end sub
    great code.
    Tks bro!

  4. #4
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    501

    Re: get value from web page

    You're welcome.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Re: get value from web page

    Quote Originally Posted by luca90 View Post
    great code.
    Tks bro!
    OPS....

    not important the line in red, can you modify the code.?
    Tks
    Attached Images Attached Images  

  6. #6
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    501

    Re: get value from web page

    remove the code that handles RowNum=1

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Re: get value from web page

    Quote Originally Posted by jdelano View Post
    remove the code that handles RowNum=1
    Not understand, sorry
    ???

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,481

    Re: get value from web page

    Are you serious??

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Re: get value from web page

    Quote Originally Posted by Arnoutdv View Post
    Are you serious??
    Yes, not understand.
    Sorry

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,855

    Re: get value from web page

    Quote Originally Posted by luca90 View Post
    Yes, not understand.
    Sorry
    Have you even looked at the code you were given, or did you just paste it without even attempting to understand it?

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,481

    Re: get value from web page

    In the given code is a line which starts with ElseIf rownum = 1, the code below this line needs to be removed

  12. #12
    Addicted Member
    Join Date
    Nov 2016
    Location
    Italy
    Posts
    205

    Re: get value from web page

    If you don't feel like modifying the code provided to you by jdelano, you can always mask the 1 line at the end of the "Command1_Click" code by adding
    Code:
    grdData.RowHeight(1) = 0
    just before End Sub.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,757

    Re: get value from web page

    Quote Originally Posted by jdelano View Post
    You need to reference Microsoft HTML Object Library & Microsoft XML, 3.0

    You didn't mention what value you wanted to grab; this just loops the known HTML of the web page and fills a MSFlexGrid control.

    Code:
    Private Sub Command1_Click()
        
        Dim HTTPRequest As MSXML2.XMLHTTP
        Dim url As String
        Dim htmlDoc As HTMLDocument
    
        Dim tbl As HTMLTable
        Dim tblRow As HTMLTableRow
        Dim tblCol As HTMLTableCol
        
        Dim rowNum As Integer
        Dim colNum As Integer
        Dim innerColNum As Integer
        
        Dim grdDataColValue As String
        
        Set HTTPRequest = New MSXML2.XMLHTTP
        Set htmlDoc = New HTMLDocument
        
        ' Open the HTTP request and send it
        url = "http://www.lottoconsult.it/estrazioni_lotto.asp?anno=2025"
        
        HTTPRequest.Open "GET", url, False
        HTTPRequest.send
    
        htmlDoc.body.innerHTML = HTTPRequest.responseText
        
        ' extract the table
        innerColNum = 1
        Set tbl = htmlDoc.getElementsByTagName("table")(7) ' examine the page source to see which table holds the data to get as it doesn't have an id
        If Not tbl Is Nothing Then
            
            ' find all the table rows
            For Each tblRow In tbl.getElementsByTagName("tr")
        
                If rowNum = 0 Then
                    ' count the columns needed for the grid - first header row
          
                    ' this site doesn't use "th" for the table header
                    ' count the column headers
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        colNum = colNum + 1
                    Next tblCol
               
                    ' set the number of columns
                    grdData.cols = colNum
                    
                    ' write the column headers
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        grdData.TextMatrix(0, colNum) = tblCol.innerText
                        grdData.ColWidth(colNum) = 1250
                        colNum = colNum + 1
                    Next tblCol
    
                ElseIf rowNum = 1 Then
                    ' second header row
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        If colNum < 2 Then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grdDataColValue = grdDataColValue & tblCol.children(0).innerText & " "
                            colNum = colNum + 1
                        Else
                            If colNum = 2 Then
                                ' write the first 2 cols to col 1 of the grid
                                grdData.TextMatrix(rowNum, 0) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                            grdDataColValue = grdDataColValue & tblCol.innerText & " "
                            innerColNum = innerColNum + 1
                            
                            If innerColNum = 6 Then
                                ' once the 5 numbers are found for the column, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grdData.TextMatrix(rowNum, colNum - 2) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                        End If
                        
                    Next tblCol
                Else
                    ' add the row of data to the grid
                    grdData.rows = grdData.rows + 1
                   
                    colNum = 0
                    For Each tblCol In tblRow.getElementsByTagName("td")
                        If colNum < 2 Then
                            ' second row of headers - column 1 & 2 are in the first column of row 1
                            grdDataColValue = grdDataColValue & tblCol.children(0).innerText & " "
                            colNum = colNum + 1
                        Else
                            If colNum = 2 Then
                                ' write the first 2 cols to col 1 of the grid
                                grdData.TextMatrix(rowNum, 0) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                        
                            ' all other rows have 5 numbers per column
                            grdDataColValue = grdDataColValue & tblCol.innerText & " "
                            innerColNum = innerColNum + 1
                            
                            If innerColNum = 6 Then
                                ' once the 5 numbers are found, write it to the grid, clear the var
                                ' reset the inner column count and move to the next column
                                grdData.TextMatrix(rowNum, colNum - 2) = grdDataColValue
                                grdDataColValue = ""
                                innerColNum = 1
                                colNum = colNum + 1
                            End If
                            
                        End If
                        
                    Next tblCol
    
                End If
                
                rowNum = rowNum + 1
                
                DoEvents
            Next tblRow
        
        End If
            
        ' release the objects
        Set HTTPRequest = Nothing
        Set htmlDoc = Nothing
        
        lblStatus.Caption = "Extracted " & CStr(grdData.rows) & " rows from table": DoEvents
    End Sub
    hi bro...
    your code dont work now!
    Peraphs the site is changed?
    Table 7 not exists now!

  14. #14
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,855

    Re: get value from web page

    Quote Originally Posted by luca90 View Post
    hi bro...
    your code dont work now!
    Peraphs the site is changed?
    Table 7 not exists now!
    That is why it isn't recommended to try and scrape websites, sooner or lautet the HTML will change and your code will break.

    It is always better to see if the site also offers an api to get at the data, it finde an alternative api instead of scraping the website.

  15. #15
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    501

    Re: get value from web page

    You'll need to inspect the page / view the page source to see (count them) which <table> the data is in now or see if it has a unique id and use that instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width