Przemysław Cwalina
Przemysław Cwalina

Reputation: 11

getting only first <li> of <ul>

Here is the problem. I have a websites and few subpages

subpages: DAMSKIE, MĘSKIE, DZIECIĘCE, SPORT, AKCESORIA, PREMIUM, TOREBKI, WYPRZEDAŻ,

On each of it are few categorizing elemens like "Półbuty", "Klapki" etc.

I can get Subpages but i can't get the list of categorizing elements (Półbuty, Klapki etc). If list looks like: "Półbuty", "Klapki", "Obcasy" my code gets only "Półbuty", but he doesn't get "Klapki" or "Obcasy".

[Image of subpages+the list of elements im trying to get][1]

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Crawler_Shoes
{
    public class Crawl
    {
        private static string navBar = "megamenu__item";
        private const string shoesTypes = "sidebar-section__wrapper sidebar-section__wrapper--categories";
        private static string mainSite = "https://www.eobuwie.com.pl/";
        public static List<string> categoriesNames = new List<string>();
        public static List<string> linksNames = new List<string>();
        public static List<string> categoriesOfCategoriesNames = new List<string>();
        private readonly List<Shoes> shoes = new List<Shoes>();

        public static async Task<IEnumerable<HtmlNode>> HttpClient(string site, string descendant, string equals)
        {
            var httpClient = new HttpClient();
            var html = await httpClient.GetStringAsync(site);
            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);
            return htmlDocument.DocumentNode.Descendants(descendant)
                .Where(node => node.GetAttributeValue("class", "").Equals(equals)).ToList();
        }
        public static async Task GetCategories()
        {
            var menu = await HttpClient(mainSite, "li", navBar);                      
            foreach (var nav in menu)
            {
                //links.Add(nav.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value);
                categoriesNames.Add(nav.Descendants("a").FirstOrDefault().InnerText); //gets names of categories
                linksNames.Add(nav.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value); //gets links for categories
                if(categoriesNames.Last() == "\n\t\t\tWyprzedaż\t\t")
                {
                    categoriesNames.Remove(categoriesNames.Last());
                    linksNames.Remove(categoriesNames.Last());
                }
            }
            Crawl.GetCategoriesofCategories();
        }
        public static async Task GetCategoriesofCategories()
        {
                for (var i = 0; i <= categoriesNames.Count-1; i++)
                {
                    var categories = await HttpClient(linksNames.ElementAt(i), "ul", shoesTypes);
                    categoriesOfCategoriesNames.Add(categoriesNames.ElementAt(i));
                    foreach(var li in categories)
                    {
                        categoriesOfCategoriesNames.Add(li.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value);
                    }
                }

        }
    }
}

The part with the problem:

    public static async Task GetCategoriesofCategories()
            {
                    for (var i = 0; i <= categoriesNames.Count-1; i++)
                    {
                        var categories = await HttpClient(linksNames.ElementAt(i), "ul", shoes

Types);
                    categoriesOfCategoriesNames.Add(categoriesNames.ElementAt(i));
                    foreach(var li in categories)
                    {
                        categoriesOfCategoriesNames.Add(li.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value);
                    }
                }

        }

Upvotes: 1

Views: 189

Answers (1)

Lucax
Lucax

Reputation: 407

I had success with this:

string url = "https://www.eobuwie.com.pl/damskie.html";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(url);
var sidebar = doc.DocumentNode.SelectSingleNode("//ul[@class='sidebar-section__wrapper sidebar-section__wrapper--categories']");
var categories = sidebar.SelectNodes("li");
foreach (var category in categories)
{
    var anchor = category.SelectSingleNode("a");
    string shoeCategory = anchor.InnerText.Trim();
    Console.WriteLine(shoeCategory);
}

It's a bit different than how you're doing it, but I at least hope you'll be able to take some hints from this and apply it to your own code.

If you need the links as well, add this:

string shoeCategoryLink = anchor.GetAttributeValue("href", string.Empty);

Upvotes: 1

Related Questions