0

The error point is For each node as htmlnode ...

Error: 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

The expected results to be got:

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 
Hoh
  • 1,196
  • 1
  • 12
  • 30
Trader
  • 27
  • 6
  • possible duplicate of [Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?](http://stackoverflow.com/questions/18241029/why-does-my-xpath-query-scraping-html-tables-only-work-in-firebug-but-not-the) – Jens Erat Oct 12 '14 at 17:47

1 Answers1

1

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

Don't be misled by your web browser which fixes the document to include missing elements like tbody.

EDIT: to get the cells one by one enumerate the rows then the cells:

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
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
  • Thanks, I got it. I could got those strings but each row are all combined into one row. I could not make use of that string for further processing, any ways that I could get each item seaprately or store into an array using the xpath: htmlDoc.DocumentNode.SelectNodes("/html/body/table[4]/tr/td/table/tr/td/table[2]? say a loop? Thanks 15:19:407.075K525K75 15:01:547.0583K585.15K83 15:01:337.04116K816.64K116 15:01:337.0377K541.31K77 ... – Trader Aug 23 '14 at 03:37