Htmlagilitypack - 從同一行讀取兩個不同的節點並將它們添加到listview

html html-agility-pack html-table selectnodes vb.net

我試圖從列表中獲取所有參與者名稱值(span class = text)以及他們的短名稱值(class = contenu),並將它們添加到listview項目的不同coloumn中,該列表將顯示為網頁的similiart 。用戶可以看到參與者的全名,也可以單擊個人參與者品牌(短名稱)以獲取更多詳細信息。

Imports HtmlAgilityPack

Public Class Form1

Dim web As New HtmlWeb
Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
Dim ParticipantNodesShort As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
Dim ParticipantNodesLong As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte']")


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' Adding ListView Columns
    ListView2.Columns.Add("Participant's contact - Full Name", 250, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)

End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

          Dim web As New HtmlWeb
    Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
    Dim ParticipantNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte'] | /html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
    Dim participantname(20) As String
    Dim brandname(50) As String
    Dim participantcount As Integer = 0
    Dim brandcount As Integer = 0

    For Each item As HtmlNode In ParticipantNodes
        If item.Name = "span" Then
            Debug.Print(participantname(participantcount))
            participantname(participantcount) = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
            participantcount = participantcount + 1
        Else
            If Not item.Attributes.Count = 3 Then
                brandname(brandcount) &= String.Format(" [{0}]", item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, ""))
                brandcount = brandcount + 1
            End If
        End If
        ListView2.Items.Add(participantname(participantcount) & " " & brandname(brandcount))

    Next
End Sub
End Class

盡量簡化問題;

這是整頁的一部分,顯示參與者名稱及其品牌(其中一些有一個,另一些有兩個,三個或四個)

如下; Mekar srl(參與者名稱)有4個不同的品牌,用逗號分隔(Eden,Mekar VENCO,Venticlima)

MEKAR srl(EDEN,MEKAR,VENCO,VENTILCLIMA)

但有些只有一個;

如下例所示; Menegra Gmbh(參與者名稱)只有一個品牌,如Menegra MENERGA GmbH(Menerga)

所以我會嘗試將這些數據與頁面幾乎相同的邏輯寫入listview;

第一列將列出參與者名稱,並且第二列中的同一行將列出品牌名稱(或者如果同一行中的多於一列,第三列和第四列)

無論從列表視圖如何,我都需要了解如何保存從網頁獲取的數據以記錄數組類型字符串並在稍後的列表視圖中顯示它們。

使用上面的代碼我連續讀取所有節點並丟失它們之間的關係(如多個品牌的參與者名稱)

希望這個聲明會更清楚,我也希望再次得到更多的支持

問候

阿里

一般承認的答案

好的,我在ListView上做了一些快速的Google搜索...這裡是結果。

在此處輸入圖像描述

Imports HtmlAgilityPack

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ListView2.Columns.Add("Participant's contact - Full Name", 250, HorizontalAlignment.Left)
    ListView2.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim web As New HtmlWeb
    Dim doc As HtmlDocument = web.Load("http://www.eurovent-certification.com/en/Certified_products/Access_by_programme.php?rub=04&srub=01&ssrub=&lg=en&select_prog=AHU")
    Dim ParticipantNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//span[@class='texte'] | /html/body/table/tr/td[2]/table[4]/tr[2]/td[2]//a[@class='contenu']")
    Dim ColumnCount As Integer = 1
    Dim TempListItem As New ListViewItem
    For Each item As HtmlNode In ParticipantNodes
        If item.Name = "span" Then
            ColumnCount = 1
            Dim Name As String = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
            TempListItem = ListView1.Items.Add(Name)
        Else
            If Not item.Attributes.Count = 3 Then
                ColumnCount += 1
                If ColumnCount > ListView1.Columns.Count Then
                    ListView1.Columns.Add("Brand Name", 100, HorizontalAlignment.Left)
                End If
                Dim SubName As String = item.InnerText.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "")
                TempListItem.SubItems.Add(SubName)
            End If
        End If
    Next
    Dim breaker = 0
End Sub

End Class

這是輸出的一部分:

 [Menu] [AHU]
List of participants to the certification programme for  Air Handling Units (AHU)  :
A.T.C Air Trade Centre Havaland?rma Sistemleri San. ve Tic. Ltd. ?ti. [ATC]
ACS KLIMA Imalat Sanayi ve Ticaret Ltd. Sti [ACS]
AERA Iklimlendirme Teknolojileri San. ve Tic AS [AERA]
AIRCALO [AIRCALO]
AIRLAN INDUSTRIAL S.A. [AIRLAN]
Airtechnic Hatzoudis E.P.E. [AIRTECHNIC]
AL SALEM YORK Manufacturing Co,Ltd [YORK]
AL-KO THERM GmbH [ALKO]
ALARKO CARRIER San. Tic. A.S. [ALARKO] [CARRIER]

我沒有使用listview那麼多,所以我不熟悉它,因為你有不同數量的列,我會暫時將這些結果存儲在某處,然後檢查我需要製作多少列。然後將結果轉儲到listview中。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow