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