zoukankan      html  css  js  c++  java
  • 获取指定网址下的html页面中的图片文件

            // 获取指定网址下的html页面中的图片文件路径列表
            private static List<string> getImageUrlList(string strSource)
            {
                Regex reg = new Regex(@"http://\S+\.(gif|bmp|png|jpg)");
                MatchCollection mc = reg.Matches(strSource);

                List<string> strList = new List<string>();

                for (int i = 0; i < mc.Count; i++)
                {
                    strList.Add(String.Format("{0}\r\n", mc[i].Value));
                }
                List<string> newList = new List<string>();
                newList.AddRange(strList.Distinct());
                return newList;
            }

            // 获取指定url路径的图片,保存下来
            private static void getUrlImage(string imageUrl)
            {
                try
                {
                    int lastIndex = imageUrl.LastIndexOf('/');
                    string fileName = imageUrl.Substring(lastIndex + 1); // 文件名称

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream receiveStream = response.GetResponseStream(); // 网络stream, 不可获取长度
                   
                    BinaryReader reader = new BinaryReader(receiveStream);
                    string imagePath = @".\img\";
                    if (!Directory.Exists(imagePath))
                        Directory.CreateDirectory(imagePath);
                    using (FileStream stream = new FileStream( imagePath + fileName, FileMode.Create))
                    {
                        byte[] buffer = new byte[256]; // 通过缓存取得数据
                        int count = reader.Read(buffer, 0, 256);
                        while (count > 0)
                        {
                            stream.Write(buffer, 0, count);
                            count = reader.Read(buffer, 0, 256);
                        }
                        stream.Flush();
                    }
                    receiveStream.Close();
                }
                catch { }

            }

            // 调用示例
            private void captureImageByUrl(string urlStr)
            {
                string strSource = getUrlHtml(urlStr);

                if (strSource == String.Empty) return;

                List<string> strList = getImageUrlList(strSource);

                StringBuilder sb = new StringBuilder();       
                foreach (string item in strList)
                {
                    sb.Append(item);
                    getUrlImage(item.TrimEnd(new char[] { '\r', '\n' }));
                }
                MessageBox.Show(sb.ToString());
            }

    ~做事情贵在坚持~
  • 相关阅读:
    清除System.Web.HttpRuntime.Cache缓存
    C# 自动添加文件头注释的方法
    MVC中为站点添加是否开启过滤器
    css 设置页面打印分页
    MVC core TempData Session has not been configured for this application or request.
    abp .net core hangfire JobStorage.Current property value has not been initialized
    数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份
    无法执行程序。所执行的命令为 "C:WindowsMicrosoft.NETFramework64v4.0.30319csc.exe" /noconfig /fullpaths @"C:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files oot411ea3248a9fbaun5r0xd.c
    ABP select2 在模态框中搜索框无法输入
    asp access 数据库连接失败(未指定的错误)
  • 原文地址:https://www.cnblogs.com/csMapx/p/2207276.html
Copyright © 2011-2022 走看看