从表中获取2列

时间:2014-02-20 11:05:53

标签: asp.net .net vb.net html-agility-pack

我想使用HTMLagility包从表中获取3个列

<table id="inner" border="1" width="25"><h3>20:25 07/02/2014
 S3
</h3><th>Pred</th><th>Act</th><th>Name</th><th>Age</th><th>Odds</th><tr><td>1 (44.8)</td><td>2</td><td align ="center" width="8">Grandads Rifle</td><td>50</td><td>6/4F</td></tr>
<tr><td>2 (39.1)</td><td>3</td><td align ="center" width="8">Rosario Girl</td><td>33</td><td>3/1</td></tr>
<tr><td><b>3 (38.6)</b></td><td><b>1</b></td><td align ="center" width="8"><b>Zulu Hondo</b></td><td><b>33</b></td><td><b>6/1</b></td></tr>
<tr><td>4 (37.6)</td><td>6</td><td align ="center" width="8">Lethal Tiger</td><td>53</td><td>9/4</td></tr>
<tr><td>5 (33.4)</td><td>4</td><td align ="center" width="8">Redstone Bo Dhu</td><td>33</td><td>6/1</td></tr>
<tr><td>6 (27.8)</td><td>5</td><td align ="center" width="8">Corgrigg Candy</td><td>25</td><td>8/1</td></tr>
</table id="inner">

从上面我只想选择0,1和0列。表中有4个。

我试过这样做我的代码:

Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports HtmlAgilityPack
Partial Class Grayhounds_home
    Inherits System.Web.UI.Page
    '  Gettodaysmatches(cominguptable, "https://www.betfair.com/sport/football?selectedTabType=TODAY", ".//div[contains(@class, 'match event-information ui-event')]", ".//span[@class='home-team-name']", ".//span[@class='away-team-name']", ".//span[@class='ui-no-score']")

    Private Sub Grayhounds_home_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim webGet As New HtmlWeb() 'open the system
        Dim doc As HtmlDocument = webGet.Load("http://www.greyhoundpredictions.co.uk/results.php") '' get the html from the webpage

        Dim tables As HtmlNodeCollection = doc.DocumentNode.SelectNodes(".//table[@id='inner']")
        Dim rows As HtmlNodeCollection = tables(0).selectNodes(".//tr")

        Dim makeSpace As String = vbLf & vbLf

        For i As Integer = 1 To rows.Count - 1
            Dim cols As HtmlNodeCollection = rows(i).SelectNodes(".//td")

            Dim Pred As String = cols(0).InnerText
            Dim Act As String = cols(1).InnerText
            Dim Odds As String = cols(4).InnerText


            Console.WriteLine("Pred : " & Pred & vbLf)
            Console.WriteLine("actual : " & Act & vbLf)
            Console.WriteLine("Odds : " & Odds & vbLf)

            Console.WriteLine(makeSpace)
        Next

    End Sub
End Class

但确实可以拉回任何东西

1 个答案:

答案 0 :(得分:0)

您可以使用索引选择第n个节点,并使用|用于选择多个节点:

doc.DocumentNode.SelectNodes("//table[@id='inner']//tr//td[0] | //table[@id='inner']//tr//td[1] | //table[@id='inner']//tr//td[4]")