码迷,mamicode.com
首页 > Windows程序 > 详细

在C#中使用正则表达式筛选出图片URL并下载图片URL中的图片到本地

时间:2016-06-04 20:48:09      阅读:1585      评论:0      收藏:0      [点我收藏+]

标签:

    本功能主要用到的知识点如下:

   1、正则表达式

   2、C#中下载文件功能的实现

   3、泛型集合的使用

   4、进程的简单操作(用于结束当前程序)

       下面就简单说一下是如何使用这些知识点的。先详细说下这个程序主要实现的功能是什么,现有一个文本文件里面都是从网页上复制下来的源代码。现需要将其中的以http、https、ftp开头,以.jpg,.png,.gif开头的图片URL地址筛选出来,并去访问这些链接,将URL中所对应的图片下载下来。经过分析后。决定使用正则表达式筛选URL地址。并使用WebClient类去实现下载的功能。代码如下:

技术分享
  1 using System.Text.RegularExpressions;
  2 using System;
  3 using System.Net;
  4 using System.IO;
  5 using System.Diagnostics;
  6 using System.Collections.Generic;
  7 namespace URLRegex
  8 {
  9     class Program
 10     {
 11         public static List<string> getUrl(string data)
 12         {
 13             List<string> strUrl= new List<string>();//定义泛型,用于存放抓取的URL
 14             string regexStr = @"(http|ftp|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)+\.(png|jpg|gif)";//查找URL的正则表达式
 15             Regex reg = new Regex(regexStr, RegexOptions.IgnoreCase);//正则表达式的类实例化
 16             MatchCollection mc = reg.Matches(data);//进行匹配
 17             if (mc.Count <= 0)//判断没有抓取到一条合法的URL
 18             {
 19                 Console.WriteLine("未抓取到符合条件的URL,按任意键退出程序");
 20                 Console.ReadKey();
 21                 Process.GetCurrentProcess().Kill();
 22             }
 23             for (int i = 0; i < mc.Count; i++)
 24             {
 25                 strUrl.Add(mc[i].Groups[0].Value);//将匹配的数据装入泛型集合
 26             }
 27             return strUrl;//返回这个泛型集合
 28 
 29         }//得到URL
 30 
 31         public static void downLoad(List<string> tempUrl)
 32         {
 33 
 34             string currentPath = System.Environment.CurrentDirectory;//得到当前目录
 35             Directory.CreateDirectory(currentPath + @"\photos\");//在当前目录下创建photos文件夹
 36             string currentPathPhotos = currentPath + @"\photos\";//得到photos的路径
 37 
 38             WebClient myDownload = new WebClient();//实例化webclient类,用于下载
 39             int i = 1; //用于图片的命名
 40             Regex regJPG = new Regex(".jpg", RegexOptions.RightToLeft);//判断图片是不是.jpg格式
 41             Regex regPNG = new Regex(".png", RegexOptions.RightToLeft);//判断图片是不是.png格式
 42 
 43             foreach (string temp in tempUrl)//遍历获取到的图片URL,并下载和保存
 44             {
 45                 Match mJpg = regJPG.Match(temp);
 46                 if (mJpg.Success)
 47                 {
 48                     string filePathJpg = currentPathPhotos + i + ".jpg";
 49                     try
 50                     {
 51                         myDownload.DownloadFile(temp, filePathJpg);
 52                         Console.WriteLine("下载成功");
 53                         i++;
 54                     }
 55                     catch
 56                     {
 57                         Console.WriteLine("下载失败");
 58                     }
 59 
 60                 }
 61                 else
 62                 {
 63                     Match mPng = regPNG.Match(temp);
 64 
 65                     if (mPng.Success)
 66                     {
 67                         string filePathPng = currentPathPhotos + i + ".png";
 68                         try
 69                         {
 70                             myDownload.DownloadFile(temp, filePathPng);
 71                             Console.WriteLine("下载成功");
 72                             i++;
 73                         }
 74                         catch
 75                         {
 76                             Console.WriteLine("下载失败");
 77                         }
 78 
 79                     }
 80                     else
 81                     {
 82                         string filePathgif = currentPathPhotos + i + ".gif";
 83                         try
 84                         {
 85                             myDownload.DownloadFile(temp, filePathgif);
 86                             Console.WriteLine("下载成功");
 87                             i++;
 88                         }
 89                         catch
 90                         {
 91                             Console.WriteLine("下载失败");
 92                         }
 93                     }
 94 
 95                 }
 96 
 97             }
 98 
 99             Process.Start("explorer", currentPathPhotos);//完成后立即呈现结果
100         }//实现下载
101 
102         public static void Main()
103         {
104                string currentPath = Environment.CurrentDirectory;   
105                string source= File.ReadAllText(currentPath+@"\test.txt");//读入文件
106                List<string> temp = getUrl(source);//筛选URL
107                Console.WriteLine("筛选后的URL地址如下:");
108                foreach (string t in temp)
109               {
110                 Console.WriteLine(t.ToString());//输入URL
111                }
112                Console.WriteLine("正在下载图片……");
113                downLoad(temp);//下载图片
114                Console.WriteLine("\n下载结束,按任意键退出");
115                Console.ReadKey();
116         }//主函数
117     }
118 }
View Code

 

难点是:

1、正则表达式的构建,因为才接触到正则表达式,所以对于其正则表达式的构建不是很熟悉,自己也在百度了查了不少的资料。也看过别人的写的一些相似的正则表达式后。才写出了这个正则表达式。

2、异常的处理。比如文件打开失败,下载失败。未得到正确的URL地址等等。(解决方案:添加上try和catch在catch中用到了当前进程的结束)。

在C#中使用正则表达式筛选出图片URL并下载图片URL中的图片到本地

标签:

原文地址:http://www.cnblogs.com/ZERO-TAO/p/5559420.html

(0)
(3)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!