1

I'm trying to scrap the list of music for a given date from the billboard website. When I obtain a list of div for each music I'm trying to get the title and the artist for each div, but in my foreach loop I get always the same return values, is like my foreach loop doesn't go to the next div.

using HtmlAgilityPack;
using System;
using System.Collections.Generic;


namespace Billboard_Scraping
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var billBoardMusic = GetMusicList("https://www.billboard.com/charts/hot-100/2000-08-12");
        }

        static List<Music> GetMusicList(string url)
        {
            var musics = new List<Music>();
            
            HtmlWeb web = new HtmlWeb();
            HtmlDocument document = web.Load(url);
            HtmlNodeCollection linknode = document.DocumentNode.SelectNodes("//div[contains(@class,\"o-chart-results-list-row-container\")]");


            foreach (var link in linknode)
            {
                var music = new Music();
                var titleXPath = "//h3[contains(@class,\"c-title\")]";
                var artistXPath = "//span[contains(@class,\"c-label  a-no-trucate\")]";

                music.Title = link.SelectSingleNode(titleXPath).InnerText.Trim();
                music.Autor = link.SelectSingleNode(artistXPath).InnerText.Trim();
                musics.Add(music);
            }
            return musics;
        }
    }

    public class Music
    {
        public string Title { get; set; }
        public string Autor { get; set; }
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you Alexei Levenkov I got it now, I had forgotten the point in the titleXPath and artistXPath strings: var titleXPath = ".//h3[contains(@class,\"c-title\")]"; var artistXPath = ".//span[contains(@class,\"c-label a-no-trucate\")]"; – Marco Favaretto May 03 '22 at 18:34

0 Answers0