WebScraper C# + htmlagilitypack

Viewed 95

i started develop application. Me need take some informations from website after download it in database, and after I need treatment this informations. Well, I don't have enough experience and be grateful any of your recomendation. for example - I'll work with sport site. (https://terrikon.com/football/spain/championship/) I need receive informations from table and download this data in DB. I tried some ways download data and understand that best way - use "htmlagilitypack". I read documentation about work with this library and best that i did:

using System;
using System.Xml;
using HtmlAgilityPack;

namespace Parser
{
    class Program
    {
        static void Main(string[] args)
        {
            var html = @"https://terrikon.com/football/spain/championship/";

            HtmlWeb web = new HtmlWeb();

            var htmlDoc = web.Load(html);

            var node = htmlDoc.DocumentNode.SelectSingleNode("//head/title");

            var table = htmlDoc.QuerySelector("#champs-table > table");

            var tableRows = table.QuerySelectorAll("tr");

            foreach (var row in tableRows)
            {
                var team = row.QuerySelector(".team"); 
                var win = row.QuerySelector(".win");
                var draw = row.QuerySelector(".draw");
                var lose = row.QuerySelector(".lose");
                Console.WriteLine(team.OuterHtml );
            };

        }
    }
}

I can receive title of website or all informations, if I'll change this string

 var node = htmlDoc.DocumentNode.SelectSingleNode("//head");

Could you give me advice how can I get informations only from table? thanks for tour attention enter image description here

1 Answers

I would recommend to also install a Css selector extension for HtmlAgilityPack which can be found here:

https://github.com/hcesar/HtmlAgilityPack.CssSelector

With that you can Query your nodes with css selectors. To get the information from that table you will have to know the CSS selector for it. In this case its:

#champs-table > table

So to get whole table you could do it like this:

var table = htmlDoc.QuerySelector("#champs-table > table");
// then query rows of that table:

var tableRows = table.QuerySelectorAll("tr");

// Now each element it tableRows is a <tr> from that html table
// you could access every value in a for each loop
foreach(var row in tableRows)
{
  var team = row.QuerySelector(".team"); // "team" is a css class applied to <td> containing the team name
  var win = row.QuerySelector(".win");
  var draw = row.QuerySelector(".draw");
var lose = row.QuerySelector(".lose");

}
Related