使用显示交易数据的股票网站的xPath(htmlagilitypack)错误的selectnodes(vb.net)

时间:2014-08-22 19:25:52

标签: vb.net parsing xpath

错误点为For each node as htmlnode ...

错误:Additional information: Object reference not set to an instance of an object.

    Dim webClient As New System.Net.WebClient
    Dim WebSource As String = webClient.DownloadString("http://boc.quotepower.com/web/bochk/stocks_mktTransactions.jsp?lang=en&stock=811&lotsize=100&searchType=2&turnover=500000&rangeType=1&begin_hour=9&begin_min=30&end_hour=16&end_min=0&lang=zh_TW&domain=BOCHK&rand=-1076862387&lastLevel1Name=nav_stocks&lastStock=00005&x=0&y=0") '
    txtPageHTML.Text = WebSource

    Dim links As New List(Of String)()
    Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
    htmlDoc.LoadHtml(WebSource)
    '/html/body/table[4]/tbody/tr/td/table/tbody/tr/td/table[2]/tbody
    '/html/body/table[4]/tbody/tr/td/table/tbody/tr/td/table[2]/tbody/tr[2]
    For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("/html/body/table[4]/tbody/tr/td/table/tbody/tr/td/table[2]/tbody/tr[2]")
        txtPageHTML = node.InnerText
    Next

预期结果:

Time    Price   Volume  Turnover    No. of Lots     Above/Below Market Price
15:19:40    7.0     75K     525K    75  
15:01:54    7.05    83K     585.15K 83  
15:01:33    7.04    116K    816.64K 116 

1 个答案:

答案 0 :(得分:1)

应为htmlDoc.DocumentNode.SelectNodes("/html/body/table[4]/tr/td/table/tr/td/table[2]/tr[2]")

不要被您的网络浏览器误导,该浏览器修复了文档以包含tbody等缺失的元素。

编辑:逐个获取细胞,然后列出细胞:

For Each row As HtmlNode In htmlDoc.DocumentNode.SelectNodes("/html/body/table[4]/tr/td/table/tr/td/table[2]/tr")
    For Each cell As HtmlNode In row.SelectNodes("td")
        'Do something with cell.InnerText
    Next
Next