0

我想要做的是从这个表中获取数据:

<div class="fliptable">
<div class="head">
    <div class="outer">
        <div class="inner">
            Information</div>
    </div>
</div>
<table class="body">
    <tbody>
        <tr>
            <td class="key" style="">
                SKU
            </td>
            <td class="right">
                SKU1<br>
                SKU2<br>
                SKU3<br>
            </td>
        </tr>
        <tr>
            <td class="key" style="">
                EAN
            </td>
            <td class="right" style="">
                EAN1<br>
                EAN2<br>
                EAN3<br>
            </td>
        </tr>
    </tbody>
</table>

我到目前为止的代码:

 Dim doc As New HtmlAgilityPack.HtmlDocument()
    doc.Load(WebBrowser1.DocumentStream)

    Try

        Dim key = doc.DocumentNode.SelectSingleNode("//div[@class='fliptable']//table[@class='body']//tr//td[@class='key']")
        RichTextBox1.AppendText(key.InnerText & vbNewLine)

        Dim right As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='fliptable']//table[@class='body']//tr//td[@class='right']")
        For Each p As HtmlNode In right.SelectNodes("//div[@class='fliptable']//table[@class='body']//tr//td[@class='right']//br")
            RichTextBox1.AppendText(p.PreviousSibling.InnerText.Trim() & vbNewLine)
        Next


    Catch ex As Exception

    End Try

我得到的结果是:

SKU
SKU1
SKU2
SKU3
EAN1
EAN2
EAN3

我想要什么:

SKU
SKU1
SKU2
SKU3
EAN
EAN1
EAN2
EAN3

但我不能让我的生活让它发挥作用

4

1 回答 1

0

这有效:

Dim bodyTable = (From tbl In doc.DocumentNode.Descendants("table")
                Where tbl.Attributes.Contains("class") AndAlso tbl.Attributes("class").Value = "body").FirstOrDefault
If bodyTable IsNot Nothing Then
    Dim allTd = bodyTable.SelectNodes("//td")
    For Each td In allTd
        For Each token In td.InnerText.Split(New String() {}, StringSplitOptions.RemoveEmptyEntries)
            RichTextBox1.AppendText(token.Trim & Environment.NewLine)
        Next
    Next
End If
于 2013-08-28T15:18:35.293 回答