本功能主要用到的知識點以下:web
一、正則表達式正則表達式
二、C#中下載文件功能的實現ide
三、泛型集合的使用函數
四、進程的簡單操做(用於結束當前程序)spa
下面就簡單說一下是如何使用這些知識點的。先詳細說下這個程序主要實現的功能是什麼,現有一個文本文件裏面都是從網頁上覆制下來的源代碼。現須要將其中的以http、https、ftp開頭,以.jpg,.png,.gif開頭的圖片URL地址篩選出來,並去訪問這些連接,將URL中所對應的圖片下載下來。通過分析後。決定使用正則表達式篩選URL地址。並使用WebClient類去實現下載的功能。代碼以下:3d
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 }
難點是:code
一、正則表達式的構建,由於才接觸到正則表達式,因此對於其正則表達式的構建不是很熟悉,本身也在百度了查了很多的資料。也看過別人的寫的一些類似的正則表達式後。才寫出了這個正則表達式。blog
二、異常的處理。好比文件打開失敗,下載失敗。未獲得正確的URL地址等等。(解決方案:添加上try和catch在catch中用到了當前進程的結束)。進程