Polarnik
Polarnik
CC#
Created by Polarnik on 10/29/2023 in #help
✅ Unsuccessfully trying to parse web pages.
I want to parse last.fm for artists but my code works only first iteration, in all iterations where pages > 1 var nodeArtist = document.DocumentNode.QuerySelectorAll("td.chartlist-name"); returns empty list which is strange because pages are identical in structure
using System.Text;
using HtmlAgilityPack;

namespace Parser
{
public class Program
{
public static async Task Main()
{
const int pageNum = 6;
string url = "https://www.last.fm/user/Polarnichek/library/artists?page=0";
var web = new HtmlWeb();
var sb = new StringBuilder(url.Length + 1);
sb.Append(url);
var artists = new List<string>(500);

for (int page = 4; page <= pageNum; page++)
{
sb.Clear();
sb.Append(url);
sb.Replace("page=0", $"page={page}");

var document = await web.LoadFromWebAsync(sb.ToString());

var nodeArtist = document.DocumentNode.QuerySelectorAll("td.chartlist-name");
foreach (var artist in nodeArtist)
{
var artistName = artist.QuerySelector("a").Attributes["title"].Value;
artists.Add(artistName);
}
}
Console.WriteLine(artists.Count);
}
}
}
using System.Text;
using HtmlAgilityPack;

namespace Parser
{
public class Program
{
public static async Task Main()
{
const int pageNum = 6;
string url = "https://www.last.fm/user/Polarnichek/library/artists?page=0";
var web = new HtmlWeb();
var sb = new StringBuilder(url.Length + 1);
sb.Append(url);
var artists = new List<string>(500);

for (int page = 4; page <= pageNum; page++)
{
sb.Clear();
sb.Append(url);
sb.Replace("page=0", $"page={page}");

var document = await web.LoadFromWebAsync(sb.ToString());

var nodeArtist = document.DocumentNode.QuerySelectorAll("td.chartlist-name");
foreach (var artist in nodeArtist)
{
var artistName = artist.QuerySelector("a").Attributes["title"].Value;
artists.Add(artistName);
}
}
Console.WriteLine(artists.Count);
}
}
}
20 replies