Scrape a table using ScrapySharp and HtmlAgilityPack

215 Views Asked by At

I am trying to scrape an economic calendar from a specific website. Actually, I tried many times without any success, I don't know where I am wrong. Can you help me, pls?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Network;

namespace Calendar
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://www.fxstreet.com/economic-calendar";
            var webGet = new HtmlWeb();
            if (webGet.Load(url) is HtmlDocument document)
            {
                var nodes = document.DocumentNode.CssSelect("#fxst-calendartable tbody").ToList();
                foreach (var node in nodes)
                {
                    // event_time
                    Debug.Print(node.CssSelect("div div").Single().InnerText);
                    // event-title
                    Debug.Print(node.CssSelect("div a").Single().InnerText);
                }
            }
            Console.WriteLine("");
            Console.ReadLine()
        }
    }
}

1

There are 1 best solutions below

2
fuzzymind On

What error are you getting? If you want to publish the event names and times from the website, I am assuming you need to read the table. You can do so using

HtmlNode tablebody = doc.DocumentNode.SelectSingleNode("//table[@class='fxs_c_table']/tbody");
    foreach(HtmlNode tr in tablebody.SelectNodes("./tr[@class='fxs_c_row']"))
    {
       Console.WriteLine("\nTableRow: ");
        foreach(HtmlNode td in tr.SelectNodes("./td"))
        {
           Console.WriteLine(td.SelectSingleNode("./span").InnerText);
        }

    }

Get hold of the table with the class attribute and then use relevant XPATH to traverse the elements. Please post the error you are getting with your code.