none
Can anyone help me how to fetch this web site data ? RRS feed

  • Question

  • I have the follow link:

    LINK

    I tried my code, cannot get the table data, because it do not have table id and with some tricky code (I think), here is my code: 

     static void Main(string[] args)
            {
                HtmlWeb webClient = new HtmlWeb();
    
                HtmlAgilityPack.HtmlDocument doc = webClient.Load("http://racing.hkjc.com/racing/Info/Meeting/Results/English/Local/20161116/HV/3");
                HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
            
    
                    var cells = (from cell in table.SelectNodes("./tr")
                                 select cell.InnerText).ToList();
    
                foreach (var cell in cells)
    
                {
                    Console.Write(cell);
                    Console.ReadKey();
                }

    Anyone know how to get the data from this page ? and any tricky code it using ?

    Thanks a lot


    Thursday, November 17, 2016 9:04 AM

All replies

  • Hi WKCALVIN,

    Thank you for posting here.

    For your question, you could use F12 to get the information from the table in your website.

    Here is the screenshot after click F12 in your keyboard.

    I use the code to get the value of this table.

    Please try the following code. I tried it. It works well.

    using HtmlAgilityPack;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace get_table_from_website
    {
        class Program
        {
            static void Main(string[] args)
            { 
                WebClient webClient = new WebClient();
                string page = webClient.DownloadString("http://racing.hkjc.com/racing/Info/Meeting/Results/English/Local/20161116/HV/3");
    
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(page);
    
                List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='tableBorder trBgBlue tdAlignC number12 draggable']")
                            .Descendants("tr")
                            .Skip(1)
                            .Where(tr => tr.Elements("td").Count() ==12)
                            .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
                            .ToList();           
            }
        }
    
    }

    I use a gif to show you the result.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regard,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 17, 2016 11:12 AM
    Moderator