需求:查找页面图片并下载至本地;
实现:
首先:读取通过网络html内容,并用正则表达式查找图片地下。
其次:使用WebRequest.Create创建图片请求。
最后:把获取图片网络流数据通过FileStream创建本地文件并写入数据。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace StreamDemo { class Program { static void Main(string[] args) { string url = "http://www.wzfzl.cn/fzl/mm/2013/0429/7092.html"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 5000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.GetEncoding("gb2312"), true); string html = sr.ReadToEnd(); //Console.Write(html); // 定义正则表达式用来匹配 img 标签 Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""‘]?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""‘<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(html); int foreachIndex = 1; foreach (Match match in matches) { string imageUrl = "http://www.wzfzl.cn/" + match.Groups["imgUrl"].Value; if (imageUrl.IndexOf("uploads") > 0) { download(imageUrl, foreachIndex); } foreachIndex++; } sr.Close(); sr.Dispose(); Console.ReadKey(); } /// <summary> /// 下载图片 /// </summary> /// <param name="imageUrl"></param> /// <param name="index"></param> static void download(string imageUrl, int index) { string filename = DateTime.Now.Second.ToString() + new Random().Next(); string filepath = @"F:\uploads\" + filename + "_" + index + ".jpg"; //FileStream FileMode.OrenOrCreate 使用写模式可以省略下面创建文件语句 //if (!File.Exists(filepath)) //{ // File.Create(filepath).Dispose(); //创建文件并关闭创建对象资源。 //} //请求图片 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl); request.Timeout = 5000; //请求超时时间 request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); //创建文件并写入数据 FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); byte[] buffer = new byte[1024]; while (true) { int r = s.Read(buffer, 0, buffer.Length); if (r == 0) { break; } fs.Write(buffer, 0, r); } fs.Close(); fs.Dispose(); response.Close(); Console.Write("Success : "); Console.WriteLine(imageUrl); } } }
运行效果如下:
时间: 2024-11-01 18:51:43