c# find image in html and download them

后端 未结 4 1998
予麋鹿
予麋鹿 2020-12-30 18:21

i want download all images stored in html(web page) , i dont know how much image will be download , and i don`t want use \"HTML AGILITY PACK\"

i search in google bu

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 18:55

    People are giving you the right answer - you can't be picky and lazy, too. ;-)

    If you use a half-baked solution, you'll deal with a lot of edge cases. Here's a working sample that gets all links in an HTML document using HTML Agility Pack (it's included in the HTML Agility Pack download).

    And here's a blog post that shows how to grab all images in an HTML document with HTML Agility Pack and LINQ

        // Bing Image Result for Cat, First Page
        string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";
    
        // For speed of dev, I use a WebClient
        WebClient client = new WebClient();
        string html = client.DownloadString(url);
    
        // Load the Html into the agility pack
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
    
        // Now, using LINQ to get all Images
        List imageNodes = null;
        imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img")
                      where node.Name == "img"
                      && node.Attributes["class"] != null
                      && node.Attributes["class"].Value.StartsWith("img_")
                      select node).ToList();
    
        foreach(HtmlNode node in imageNodes)
        {
            Console.WriteLine(node.Attributes["src"].Value);
        }
    

提交回复
热议问题