最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#實(shí)現(xiàn)網(wǎng)頁圖片提取工具代碼分享

 更新時(shí)間:2013年12月26日 10:20:31   作者:  
c#實(shí)現(xiàn)網(wǎng)頁圖片提取工具代碼分享,大家參考使用吧

復(fù)制代碼 代碼如下:

public Array MatchHtml(string html,string com)
       {
           List<string> urls = new List<string>();
           html = html.ToLower();
           //獲取SRC標(biāo)簽中的URL
           Regex regexSrc = new Regex("src=\"[^\"]*[(.jpg)(.png)(.gif)(.bmp)(.ico)]\"");
           foreach(Match m in regexSrc.Matches(html))
           {
               string src = m.Value;
               src = src.Replace("src=","").Replace("\"","");
               if (!src.Contains("http"))
                   src = com + src;
               if(!urls.Contains(src))
               urls.Add(src);
           }
           //獲取HREF標(biāo)簽中URL
           Regex regexHref = new Regex("href=\"[^\"]*[(.jpg)(.png)(.gif)(.bmp)(.ico)]\"");
           foreach (Match m in regexHref.Matches(html))
           {
               string href = m.Value;
               href = href.Replace("href=", "").Replace("\"", "");
               if (!href.Contains("http"))
                   href = com + href;
               if(!urls.Contains(href))
               urls.Add(href);
           }
           return urls.ToArray();
       }

復(fù)制代碼 代碼如下:

[DllImport("kernel32.dll")]
       static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
       [DllImport("kernel32.dll")]
       static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int mode);
       [DllImport("kernel32.dll")]
       static extern IntPtr GetStdHandle(int handle);
       const int STD_INPUT_HANDLE = -10;
       const int ENABLE_QUICK_EDIT_MODE = 0x40 | 0x80;
       public static void EnableQuickEditMode()
       {
           int mode; IntPtr handle = GetStdHandle(STD_INPUT_HANDLE);
           GetConsoleMode(handle, out mode);
           mode |= ENABLE_QUICK_EDIT_MODE;
           SetConsoleMode(handle, mode);
       }
       static void Main(string[] args)
       {
           EnableQuickEditMode();
           int oldCount = 0;
           Console.Title = "TakeImageFromInternet";
           string path = "E:\\Download\\loading\\";
           while (true)
           {
               Console.Clear();
               string countFile = "E:\\CountFile.txt";//用來計(jì)數(shù)的文本,以至于文件名不重復(fù)
               int cursor = 0;
               if (File.Exists(countFile))
               {
                   string text = File.ReadAllText(countFile);
                   try
                   {
                       cursor =oldCount = Convert.ToInt32(text);//次數(shù)多了建議使用long
                   }
                   catch { }
               }
               Console.Write("please input a url:");
               string url = "http://www.baidu.com/";
               string temp = Console.ReadLine();
               if (!string.IsNullOrEmpty(temp))
                   url = temp;
               Match mcom = new Regex(@"^(?i)http://(\w+\.){2,3}(com(\.cn)?|cn|net)\b").Match(url);//獲取域名
               string com = mcom.Value;
               //Console.WriteLine(mcom.Value);
               Console.Write("please input a save path:");
               temp = Console.ReadLine();
               if (Directory.Exists(temp))
                   path = temp;
               Console.WriteLine();
               WebClient client = new WebClient();
               byte[] htmlData = null;
               htmlData = client.DownloadData(url);
               MemoryStream mstream = new MemoryStream(htmlData);
               string html = "";
               using (StreamReader sr = new StreamReader(mstream))
               {
                   html = sr.ReadToEnd();
               }
               Array urls = new MatchHtmlImageUrl().MatchHtml(html,com);

               foreach (string imageurl in urls)
               {
                  Console.WriteLine(imageurl);
                   byte[] imageData = null;
                   try
                   {
                       imageData = client.DownloadData(imageurl);
                   }
                   catch { }
                   if (imageData != null && imageData.Length>0)
                       using (MemoryStream ms = new MemoryStream(imageData))
                       {
                           try
                           {

                               string ext = Aping.Utility.File.FileOpration.ExtendName(imageurl);
                               ImageFormat format = ImageFormat.Jpeg;
                               switch (ext)
                               {
                                   case ".jpg":
                                       format = ImageFormat.Jpeg;
                                       break;
                                   case ".bmp":
                                       format = ImageFormat.Bmp;
                                       break;
                                   case ".png":
                                       format = ImageFormat.Png;
                                       break;
                                   case ".gif":
                                       format = ImageFormat.Gif;
                                       break;
                                   case ".ico":
                                       format = ImageFormat.Icon;
                                       break;
                                   default:
                                       continue;
                               }
                               Image image = new Bitmap(ms);
                               if (Directory.Exists(path))
                                   image.Save(path + "\\" + cursor + ext, format);
                           }
                           catch(Exception ex) { Console.WriteLine(ex.Message); }
                       }
                   cursor++;
               }
               mstream.Close();
               File.WriteAllText(countFile, cursor.ToString(), Encoding.UTF8);
               Console.WriteLine("take done...image count:"+(cursor-oldCount).ToString());
           }           
       }

相關(guān)文章

最新評論

南昌县| 青冈县| 巴彦淖尔市| 双辽市| 苏尼特右旗| 郸城县| 高唐县| 东阿县| 玉龙| 岱山县| 郑州市| 陆川县| 杨浦区| 水富县| 宜章县| 三原县| 博客| 万荣县| 三河市| 伊春市| 随州市| 油尖旺区| 嘉鱼县| 青川县| 清苑县| 同江市| 苏尼特左旗| 渭南市| 鹤峰县| 太仓市| 策勒县| 株洲县| 延寿县| 霞浦县| 乐东| 开阳县| 德阳市| 上犹县| 交城县| 古蔺县| 宣武区|