zoukankan      html  css  js  c++  java
  • 批量下载图片

    1. 效果图

    2. 主要代码:

    private async void button_Click(object sender, RoutedEventArgs e)
            {
                var htmlContent = await FileDownLoader.Instance.GetAsync(webUrl.Text);
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(htmlContent);
                label1.Content = "";
                var nodeCollection=  doc.DocumentNode.SelectNodes("//img");
                if(nodeCollection!=null)
                {
                    string imgDirectory = System.IO.Path.Combine(Environment.CurrentDirectory, "Imgs");
                    if(!Directory.Exists(imgDirectory))
                    {
                        Directory.CreateDirectory(imgDirectory);
                    }
                    progressBar.Maximum = nodeCollection.Count;
                    progressBar.Value = 0;
                    int errorCount = 0;
                    foreach (var item in nodeCollection)
                    {
                        progressBar.Value += 1;
                        try
                        {
                            var imgSrc=item.GetAttributeValue("src", null);
                            MyImg img = new MyImg(imgSrc);
                            if(!string.IsNullOrEmpty(img.FileName))
                            {
                              await  FileDownLoader.Instance.DownLoadImg(img, imgDirectory);
                            }     
                        }
                        catch (Exception ex)
                        {
                            errorCount += 1;
                        }
                        label1.Content = "第"+progressBar.Value+"个图片,共"+ progressBar.Maximum+"个图片,"+errorCount+"个错误";
                    }
                    progressBar.Value = progressBar.Maximum;
    
                }
    

      

      public  class FileDownLoader
        {
            HttpClient httpClient = new HttpClient();
            public static FileDownLoader Instance = new FileDownLoader();
            public async Task DownLoadImg(MyImg img,string imgDirectory)
            {
                var imgData = await httpClient.GetByteArrayAsync(img.ImgSrc);
                string newfilename = System.IO.Path.Combine(imgDirectory, img.FileName);
                using (var stream = File.Open(newfilename, FileMode.Create))
                {
                    await stream.WriteAsync(imgData, 0, imgData.Length);
                };
            }
            public async Task<string> GetAsync(string url)
            {
             var response= await  httpClient.GetAsync(url);
                return await response.Content.ReadAsStringAsync();
            }
        }
    

     

        public class MyImg
        {
            public MyImg(string imgSrc)
            {
                if (imgSrc != null)
                {
                    var startIndex = imgSrc.LastIndexOf(@"/");
                    var startIndex1 = imgSrc.LastIndexOf(".");
                    FileName = imgSrc.Substring(startIndex + 1, startIndex1 - startIndex + 3);
                    ImgSrc = imgSrc;
                }
            }
            public string FileName { get; set; }
    
            public string ImgSrc { get; set; }
        }
    

      

  • 相关阅读:
    atom 震动特效
    CSRF和XSS
    解决remove @override annotation(jdk1.5和jdk1.6)
    JDK 工具列表
    解决Win10系统backgroundTaskHost占用cpu大
    ideaIU-15.0.2 注册码
    jprofiler_windows-x64_9_1注册码
    修改ligerui 默认确认按钮
    解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
    安装 Flex2packagebeta_1.994
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/4823456.html
Copyright © 2011-2022 走看看