How to get values ​​from a web page

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
13 posts Page 1 of 2
Contributors
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

Hello everyone,

I'm working on a program that needs to process some football predictions, do you actually have to enter the values ​​by hand and I would like the objects NumericUpDown there is a value read directly from the network.
The site that would use to withdraw the values ​​is the following:

http://www.diretta.it/classifiche/A1AAw ... le;overall

I would like to understand how do I capture the following information in the table standings:

the name of the team (I select from a comboBox, and the program has to go to read the values ​​of a line having the selected team), for example, and then select Juventus will be picked up the following values:

6 - 6 - 0 - 0 - 13 - 2 - 18

you can do this? Who can show me how to do? I've never worked with html requests and I would need an example on how to interact with this part of vb.net, would you give me a big hand.

Image example:

Image

Result like that:

Image

Thank you all, and I wish you a good day.

http://www.diretta.it/classifiche/A1AAw ... le;overall
I'm in the empire business.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

You can use this code to read the table values and show them in the program:
Code: Select all
    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click

        WebBrowser1.Url = New Uri("http://tinyurl.com/l3kn7c4")

    End Sub

    Dim PG, V, N, P, R, P2 As New List(Of String)

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        PG.Clear()
        V.Clear()
        N.Clear()
        P.Clear()
        R.Clear()
        P2.Clear()

        Dim table As HtmlElement = WebBrowser1.Document.GetElementsByTagName("tbody")(0)

        Dim rows As HtmlElementCollection = table.GetElementsByTagName("tr")

        For i As Integer = 0 To rows.Count - 1

            Dim columns As HtmlElementCollection = rows(i).GetElementsByTagName("td")

            ComboBox1.Items.Add(columns(1).InnerText) 'Squadra'

            PG.Add(columns(2).InnerText)
            V.Add(columns(3).InnerText)
            N.Add(columns(4).InnerText)
            P.Add(columns(5).InnerText)
            R.Add(columns(6).InnerText)
            P2.Add(columns(7).InnerText)

        Next

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        NumericUpDown1.Value = PG(ComboBox1.SelectedIndex)
        NumericUpDown2.Value = V(ComboBox1.SelectedIndex)

        'etc'

    End Sub
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

hey, thanks for the answer.
What's this:
Code: Select all

Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click

        WebBrowser1.Url = New Uri("http://www.diretta.it/classifiche/A1AAw29q/p4jMxzfU/#table;overall")

    End Sub

in the list errors I get:
"the handles clause requires a WithEvents variable defined in the type that contains it or in one of its base types".

Uhm, I get also an error of:
Code: Select all
Dim table As HtmlElement = WebBrowser1.Document.GetElementsByTagName ("tbody") (0) 
tells me that the index 0 is not valid and must be between 0 and -1.

perhaps it would be better to use this link:

http://it.soccerway.com/national/italy/ ... on/r27139/

Thanks.
I'm in the empire business.
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

KraZy wrote:
hey, thanks for the answer.
What's this:
Code: Select all

Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click

        WebBrowser1.Url = New Uri("http://www.diretta.it/classifiche/A1AAw29q/p4jMxzfU/#table;overall")

    End Sub

in the list errors I get:
"the handles clause requires a WithEvents variable defined in the type that contains it or in one of its base types".

Uhm, I get also an error of:
Code: Select all
Dim table As HtmlElement = WebBrowser1.Document.GetElementsByTagName ("tbody") (0) 
tells me that the index 0 is not valid and must be between 0 and -1.

perhaps it would be better to use this link:

http://it.soccerway.com/national/italy/ ... on/r27139/

Thanks.
I think getting the 'index is not valid and must be between 0 and -1' happens because the document isn't really completed loading even though the eventhandler still goes off. I would try adding an If statement around that code so it looks like this
Code: Select all
    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Not WebBrowser1.IsBusy Then
            PG.Clear()
            V.Clear()
            N.Clear()
            P.Clear()
            R.Clear()
            P2.Clear()

            Dim table As HtmlElement = WebBrowser1.Document.GetElementsByTagName("tbody")(0)

            Dim rows As HtmlElementCollection = table.GetElementsByTagName("tr")

            For i As Integer = 0 To rows.Count - 1

                Dim columns As HtmlElementCollection = rows(i).GetElementsByTagName("td")

                ComboBox1.Items.Add(columns(1).InnerText) 'Squadra'

                PG.Add(columns(2).InnerText)
                V.Add(columns(3).InnerText)
                N.Add(columns(4).InnerText)
                P.Add(columns(5).InnerText)
                R.Add(columns(6).InnerText)
                P2.Add(columns(7).InnerText)

            Next
        End If
    End Sub
That may or may not work, but it's worth a shot
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

Thanks for the support but I still get the same error.
You can try the project with your code here and see if I'm wrong I practically insertion.
However, I used the second link, because it seems to have a better html formatting, the first turning on frame in the second one has just one table.
You do not have the required permissions to view the files attached to this post.
I'm in the empire business.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

KraZy wrote:
in the list errors I get:
"the handles clause requires a WithEvents variable defined in the type that contains it or in one of its base types".
You would need to run the btnLoad_Click function in order for the code to work, also the code will only work with the link in the first post since it is specific to that page.

Maybe someone else can verify the result?
Last edited by mandai on Sat Oct 18, 2014 6:06 pm, edited 2 times in total.
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

Hi,

When I enter this function in the code tells me an error that I listed above, please take a look.
Update: i have integrate the code in a button, so it not display me the previous error but the code doesn't work, infact i obtain the previous index error ...
I'm in the empire business.
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

KraZy wrote:
Hi,

When I enter this function in the code tells me an error that I listed above, please take a look.
Update: i have integrate the code in a button, so it not display me the previous error but the code doesn't work, infact i obtain the previous index error ...
Another problem you will encounter is that you're looking at the wrong table I believe. If you're using this website then the correct table you will be looking for is the 2nd indexed one so
Code: Select all
WebBrowser1.Document.GetElementsByTagName("tbody")(2)
Also I have your program working properly. Just add a background worker and use this code
Code: Select all
Public Class Form1


    Dim Teams, PG, V, N, P, R, P2 As New List(Of String)
    Dim elementTable As HtmlElement
    Dim elementRows As HtmlElementCollection
    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Not WebBrowser1.IsBusy AndAlso Not BackgroundWorker1.IsBusy Then
            Try
                PG.Clear()
                V.Clear()
                N.Clear()
                P.Clear()
                R.Clear()
                P2.Clear()

                elementTable = WebBrowser1.Document.GetElementsByTagName("tbody")(2)
                elementRows = elementTable.GetElementsByTagName("tr")

                BackgroundWorker1.RunWorkerAsync()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        NumericUpDown1.Value = PG(ComboBox1.SelectedIndex)
        NumericUpDown2.Value = V(ComboBox1.SelectedIndex)

        'etc'

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Url = New Uri("http://it.soccerway.com/national/italy/serie-a/20142015/regular-season/r27139/")
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        For i As Integer = 0 To elementRows.Count - 1
            Dim elementColumns As HtmlElementCollection = elementRows(i).GetElementsByTagName("td")
            Dim groups As New List(Of String)
            If elementColumns.Count > 0 Then
                For Each k As HtmlElement In elementColumns
                    groups.Add(k.InnerText)
                Next
                If Not Teams.Contains(groups(2)) Then
                    Teams.Add(groups(2))
                    PG.Add(groups(3))
                    V.Add(groups(4))
                    N.Add(groups(5))
                    P.Add(groups(6))
                    R.Add(groups(7))
                    P2.Add(groups(8))
                Else
                    WebBrowser1.Stop()
                End If
            End If
        Next
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If Not BackgroundWorker1.IsBusy Then
            MessageBox.Show("Done") ' Or whatever you want to happen when all objects have been found
        End If
    End Sub
End Class
In some areas it can be shortened, but it should work fine at the moment

*EDIT
Shortened: no longer need background worker
Code: Select all
Public Class Form1


    Dim Teams, PG, V, N, P, R, P2 As New List(Of String)
    Dim elementTable As HtmlElement
    Dim elementRows As HtmlElementCollection
    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Not WebBrowser1.IsBusy Then
            Try
                PG.Clear()
                V.Clear()
                N.Clear()
                P.Clear()
                R.Clear()
                P2.Clear()

                elementTable = WebBrowser1.Document.GetElementsByTagName("tbody")(2)
                elementRows = elementTable.GetElementsByTagName("tr")

                For i As Integer = 0 To elementRows.Count - 1
                    Dim elementColumns As HtmlElementCollection = elementRows(i).GetElementsByTagName("td")
                    Teams.Add(elementColumns(2).InnerText)
                    PG.Add(elementColumns(3).InnerText)
                    V.Add(elementColumns(4).InnerText)
                    N.Add(elementColumns(5).InnerText)
                    P.Add(elementColumns(6).InnerText)
                    R.Add(elementColumns(7).InnerText)
                    P2.Add(elementColumns(8).InnerText)

                    If Teams.Count > 19 Then
                        WebBrowser1.Stop()
                    End If
                Next
                For i As Integer = 0 To Teams.Count - 1
                    ComboBox1.Items.Add(Teams(i))
                Next

            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        NumericUpDown1.Value = PG(ComboBox1.SelectedIndex)
        NumericUpDown2.Value = V(ComboBox1.SelectedIndex)

        'etc'

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Url = New Uri("http://it.soccerway.com/national/italy/serie-a/20142015/regular-season/r27139/")
    End Sub
End Class
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

Great, you're the best, you are able to figure out what I wanted and the code is really very simple and powerful so I could understand it. clapper;
There's only one small problem, the combobox, it seems that teams are added several times. How can fix this?

I then saw that the championships are uploaded to the site a list, it would be possible to get the data list such as "Series A" and load only the teams from that league?

Image example:

Image

Update:

I read on the net that with the HttpRequest would be much faster than the software, in fact I have noticed a marked slowdown of the application due to the WebBrowser control. Before proceeding, let me ask if you can get the same result with the Http Request?

Why I would make sure to get the values ​​form present in the field div, and until now I'm trying in vain. So you could opt for the Http Request, if it is not a problem for her. Thank you for your patience. Have a good Sunday.
I'm in the empire business.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

KraZy wrote:
the combobox, it seems that teams are added several times. How can fix this?
You can use ComboBox1.Items.Clear to reset the team list, this can be added in the WebBrowser1_DocumentCompleted event.
KraZy wrote:
let me ask if you can get the same result with the Http Request?
It is possible to use HttpWebRequest instead of the WebBrowser, but you would need to add code in order to parse the HTML.
13 posts Page 1 of 2
Return to “Coding Help & Support”