download all Images of a Website

后端 未结 1 1460
一个人的身影
一个人的身影 2021-02-06 19:39

So I just started learning C# last night. The first project I started was a simple Image-Downloader, which downloads all images of a website using HtmlElementCollection.

相关标签:
1条回答
  • 2021-02-06 20:09

    Take a gander at How can I use HTML Agility Pack to retrieve all the images from a website?

    This uses a library called HTML Agility Pack to download all <img src="" \> lines on a website. How can I use HTML Agility Pack to retrieve all the images from a website?

    If that topic somehow disappears, I'm putting this up for those who need it but can't reach that topic.

    // Creating a list array
    public List<string> ImageList; 
    public void GetAllImages()
    {
        // Declaring 'x' as a new WebClient() method
        WebClient x = new WebClient();
    
        // Setting the URL, then downloading the data from the URL.
        string source = x.DownloadString(@"http://www.google.com");
    
        // Declaring 'document' as new HtmlAgilityPack() method
        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    
        // Loading document's source via HtmlAgilityPack
        document.LoadHtml(source);
    
        // For every tag in the HTML containing the node img.
        foreach(var link in document.DocumentNode.Descendants("img")
                                    .Select(i => i.Attributes["src"])) 
        {
            // Storing all links found in an array.
            // You can declare this however you want.
            ImageList.Add(link.Attribute["src"].Value.ToString());
        }
    }
    

    Since you are rather new as you stated, you can add HTML Agility Pack easily with NuGet. To add it, you right-click on your project, click Manage NuGet Packages, search the Online tab on the left hand side for HTML Agility Pack and click install. You need to call it by using using HtmlAgilityPack;

    After all that you should be fine creating and using a method already created to download all items contained in the image_list array created above.

    Good luck!

    EDIT: Added comments explaining what each section does.

    EDIT2: Updated snippet to reflect user comment.

    0 讨论(0)
提交回复
热议问题