troubleshooting Question

C#: HtmlAgilityPack getting elements using Xpath

Avatar of trevor1940
trevor1940 asked on
.NET ProgrammingC#
5 Comments1 Solution145 ViewsLast Modified:
Hi
I'm trying to use HtmlAgilityPack to Travers some  HTML Test.html

Under each b-post will be a single picture, a group of pictures in a slide show or a video I'm struggling to extract the elements like Title for  each post &  img src

  class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string Root = @"H:\TopTotty\CarrieLaChance\onlyfans\Vids\";
            string CarrieOF = Root + "Test.html"; // "CarrieOF.html";
            var htmlDoc = new HtmlDocument();
            htmlDoc.Load(CarrieOF);


                var bPostNodes = htmlDoc.DocumentNode.SelectNodes(".//div[contains(@class,'b-post')]");

            string Title = "";
            string VidSrc = "";
            string Poster = "";
            foreach (var divNodes in bPostNodes)
                {
                ///html/body/div/main/div/div/div[2]/div/div[2]/div[2]/div[1]/div[1]/div/a/span
                //Title = divNodes.SelectSingleNode(".//span").Attributes["title"].Value;
                try
                {
                    ///html/body/div/main/div/div/div[2]/div/div[2]/div[5]/div[1]/div[1]/div/a/span
                    if (divNodes.SelectSingleNode(".//span").Attributes["title"] != null)
                    {
                        Title = divNodes.SelectSingleNode(".//span").Attributes["title"].Value;
                    }

                    // /html/body/main/div/div/div/div/div/div/div/div/div[1]/div[3]/div[1]/figure/div/div[2]/video/source
                    if (divNodes.SelectSingleNode(".//video/source").Attributes["src"].Value != null)
                    {

                        VidSrc = divNodes.SelectSingleNode(".//video/source").Attributes["src"].Value;
                        Uri uri = new Uri(VidSrc);

                        string LocalFile = Root + "\\" + System.IO.Path.GetFileName(uri.LocalPath);
                        if (File.Exists(LocalFile))
                        {
                            Console.WriteLine("Title  {0} , {1} ", Title, LocalFile);
                        }
                        if (divNodes.SelectSingleNode(".//video").Attributes["poster"].Value != null)
                        {
                            Poster = divNodes.SelectSingleNode(".//video").Attributes["poster"].Value;
                            Console.WriteLine("poster {0}", Poster);
                        } 
                    }


                    //  Single image
                    Console.WriteLine("Title  {0} Before  Single image", Title);
                    // /html/body/div/main/div/div/div[2]/div/div[2]/div[5]/div[3]/div[1]
                    if (divNodes.SelectSingleNode(".//div[starts-with(@class,'post_img_block'])") != null)
                    {

                        // /html/body/div/main/div/div/div[2]/div/div[2]/div[5]/div[3]/div[1]/div/img
                        var imgNode = divNodes.SelectSingleNode(".//div[starts-with(@class,'post_img_block')]");
                        string ImgSrc = imgNode.SelectSingleNode(".//img").Attributes["src"].Value;
                        Console.WriteLine("Title   {0} , Image src: {1} ", Title, ImgSrc);

                    }

                    //Slideshow

                    Console.WriteLine("Title  {0} Before  Slideshow", Title);
                    if (divNodes.SelectSingleNode(".//div[starts-with(@class,'swiper'])") != null)
                    {
                        var SlideNode = divNodes.SelectSingleNode(".//div[starts-with(@class,'swiper-slide')]");
                        var Slides = SlideNode.SelectNodes(".//img");
                        foreach (var Slide in Slides)
                        {
                        string ImgSrc = Slide.Attributes["src"].Value;
                        Console.WriteLine("Title in Slideshow {0} , Image src: {1} ", Title, ImgSrc);
                        }
                        

                    }

                }
                catch (Exception)
                {

                    continue;
                }
            }
                Console.WriteLine("I'm Done");

            
        }
    }
ASKER CERTIFIED SOLUTION
Ioannis Paraskevopoulos

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros