Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to grab a data table from a webpage and display it in a vb.NET application.

While doing some research I found out that the HTML Agility Pack contains features that will allow to easily scrape/extract data from a webpage.

The pack does not contain any documentation and I can't seem to find the right solution on the internet. Hope someone could push me in the right direction.

This is the table I'm trying to pull.

http://testapplications.net16.net/test.html[^]

This is what I found while browsing the net and trying different methods:

C#
' Load the html document
        Dim web As New HtmlWeb()
        Dim doc As HtmlDocument = web.Load("http://testapplications.net16.net/test.html")

        ' Get all tables in the document
        Dim tables As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/table")

        ' Iterate all rows in the first table
        Dim rows As HtmlNodeCollection = tables(0).SelectNodes("./tr")
        For i As Integer = 0 To rows.Count - 1

            ' Iterate all columns in this row
            Dim cols As HtmlNodeCollection = rows(i).SelectNodes("./td")
            For j As Integer = 0 To cols.Count - 1

                ' Get the value of the column and print it
                Dim value As String = cols(j).InnerText
                MessageBox.Show(value)
            Next
        Next


I get a NullReference error at the following line:

Dim rows As HtmlNodeCollection = tables(0).SelectNodes("./tr")


Object reference not set to an instance of an object.

I'm guessing this means that TR can't be found within the code of the table?

What I have tried:

Browsed the web for documentation or solutions for my problem, couldn't find any articles or tutorials.
Posted
Updated 19-May-17 23:22pm
v3

There are plenty of articles, some here, some on other sites...here is one to get you started. Next time, search harder. Most examples use C#, but it is easily converted to VB.NET.

Web scraping with progress bar[^]
 
Share this answer
 
Try this one:

<pre> Dim web As New HtmlWeb()
        Dim doc As HtmlDocument = web.Load("https://www.w3schools.com/html/html_tables.asp")

        ' Get all tables in the document
        Dim tables As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//table[@id='customers']")

        ' Iterate all rows in the first table
        Dim rows As HtmlNodeCollection = tables(0).SelectNodes("//tr")
        For i As Integer = 0 To rows.Count - 1

            ' Iterate all columns in this row
            Dim cols As HtmlNodeCollection = rows(i).SelectNodes("//td")
            For j As Integer = 0 To cols.Count - 1

                ' Get the value of the column and print it
                Dim value As String = cols(j).InnerText
                MessageBox.Show(value)
            Next
        Next
 
Share this answer
 
Comments
Member 12428601 5-Sep-18 4:42am    
actually i ihave the same question but in my case, There are TD and TR.
hows that?
as you can see the link image below i want to get the data from the TD and TR


https://ibb.co/kg1zvK

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900