copy webpage into database

Malik Asad Mahmood 126 Reputation points
2021-01-14T14:19:42.247+00:00

Hi Friends,
I am trying to copy webpage into my databases table using vb.net 2008, I am using following code but its not working

Dim Web As New HtmlAgilityPack.HtmlWeb
Dim Doc As New HtmlAgilityPack.HtmlDocument
Doc = Web.Load("http://www.digikey.ca/product-search/en?lang=en&site=ca&KeyWords=AE9912-ND")
For Each table As HtmlAgilityPack.HtmlNode In Doc.DocumentNode.SelectNodes("//*[@id='pricing']/tr/td")
MsgBox(table.InnerText)
Next

the content of webpage I want to copy as followed please help me thank you

56661-untitled.jpg

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,562 questions
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-01-22T03:26:20.913+00:00

    Hi @Malik Asad Mahmood ,
    You need to know how to use HTML Agility Pack to get data from HTML table.
    Here's an example you can refer to.

            Dim webClient As WebClient = New WebClient()  
            Dim page As String = webClient.DownloadString("https://www.psx.com.pk/market-summary/")  
            Dim doc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument()  
            doc.LoadHtml(page)  
      
            Dim lstNode As List(Of HtmlNode) = New List(Of HtmlNode)  
            Dim lstName As List(Of String) = New List(Of String)  
            Dim lstTable As List(Of DataTable) = New List(Of DataTable)  
      
            For Each thed As HtmlNode In doc.DocumentNode.SelectNodes("//thead")  
                lstNode.Add(thed.ParentNode)  
                Dim text = thed.SelectSingleNode("tr").SelectSingleNode("th").SelectSingleNode("h4").InnerText  
                Console.WriteLine(text)  
                lstName.Add(text)  
            Next  
            For i As Integer = 0 To lstNode.Count - 1  
                Dim dt As DataTable = New DataTable  
                dt.TableName = lstName(i)  
                For Each trNode As HtmlNode In lstNode(i).SelectNodes("tr")  
                    If trNode.Attributes("class") Is Nothing Then  
                        For Each colNode As HtmlNode In trNode.SelectNodes("td")  
                            dt.Columns.Add(colNode.InnerText)  
                        Next  
                    Else  
                        Dim j As Integer = 0  
                        Dim row As DataRow = dt.NewRow()  
                        For Each rowNode As HtmlNode In trNode.SelectNodes("td")  
                            row(j) = rowNode.InnerText  
                            j += 1  
                        Next  
                    dt.Rows.Add(row)  
                    End If  
                Next  
                lstTable.Add(dt)  
            Next  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful