国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

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

2020-01-24 02:58:29
字體:
來(lái)源:轉(zhuǎ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";//用來(lái)計(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 + "http://" + 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());
           }           
       }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴海县| 灵石县| 盘锦市| 朝阳区| 竹北市| 兴城市| 凉山| 临清市| 绥德县| 普兰县| 屯门区| 兴宁市| 蒙山县| 新昌县| 浙江省| 河间市| 常德市| 通城县| 青浦区| 中方县| 丰顺县| 麦盖提县| 乐至县| 寻乌县| 宜君县| 庆阳市| 阳曲县| 错那县| 曲阳县| 炉霍县| 汶川县| 宁津县| 四子王旗| 崇明县| 水富县| 阳山县| 韶关市| 凭祥市| 利辛县| 濮阳县| 武夷山市|