zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#实现网页图片提取工具代码分享

c#网页工具代码 实现 分享 图片 提取
2023-06-13 09:15:14 时间

复制代码代码如下:


publicArrayMatchHtml(stringhtml,stringcom)
      {
          List<string>urls=newList<string>();
          html=html.ToLower();
          //获取SRC标签中的URL
          RegexregexSrc=newRegex("src=\"[^\"]*[(.jpg)(.png)(.gif)(.bmp)(.ico)]\"");
          foreach(MatchminregexSrc.Matches(html))
          {
              stringsrc=m.Value;
              src=src.Replace("src=","").Replace("\"","");
              if(!src.Contains("http"))
                  src=com+src;
              if(!urls.Contains(src))
              urls.Add(src);
          }
          //获取HREF标签中URL
          RegexregexHref=newRegex("href=\"[^\"]*[(.jpg)(.png)(.gif)(.bmp)(.ico)]\"");
          foreach(MatchminregexHref.Matches(html))
          {
              stringhref=m.Value;
              href=href.Replace("href=","").Replace("\"","");
              if(!href.Contains("http"))
                  href=com+href;
              if(!urls.Contains(href))
              urls.Add(href);
          }
          returnurls.ToArray();
      }

复制代码代码如下:


[DllImport("kernel32.dll")]
      staticexternboolSetConsoleMode(IntPtrhConsoleHandle,intmode);
      [DllImport("kernel32.dll")]
      staticexternboolGetConsoleMode(IntPtrhConsoleHandle,outintmode);
      [DllImport("kernel32.dll")]
      staticexternIntPtrGetStdHandle(inthandle);
      constintSTD_INPUT_HANDLE=-10;
      constintENABLE_QUICK_EDIT_MODE=0x40|0x80;
      publicstaticvoidEnableQuickEditMode()
      {
          intmode;IntPtrhandle=GetStdHandle(STD_INPUT_HANDLE);
          GetConsoleMode(handle,outmode);
          mode|=ENABLE_QUICK_EDIT_MODE;
          SetConsoleMode(handle,mode);
      }
      staticvoidMain(string[]args)
      {
          EnableQuickEditMode();
          intoldCount=0;
          Console.Title="TakeImageFromInternet";
          stringpath="E:\\Download\\loading\\";
          while(true)
          {
              Console.Clear();
              stringcountFile="E:\\CountFile.txt";//用来计数的文本,以至于文件名不重复
              intcursor=0;
              if(File.Exists(countFile))
              {
                  stringtext=File.ReadAllText(countFile);
                  try
                  {
                      cursor=oldCount=Convert.ToInt32(text);//次数多了建议使用long
                  }
                  catch{}
              }
              Console.Write("pleaseinputaurl:");
              stringurl="http://www.baidu.com/";
              stringtemp=Console.ReadLine();
              if(!string.IsNullOrEmpty(temp))
                  url=temp;
              Matchmcom=newRegex(@"^(?i)http://(\w+\.){2,3}(com(\.cn)?|cn|net)\b").Match(url);//获取域名
              stringcom=mcom.Value;
              //Console.WriteLine(mcom.Value);
              Console.Write("pleaseinputasavepath:");
              temp=Console.ReadLine();
              if(Directory.Exists(temp))
                  path=temp;
              Console.WriteLine();
              WebClientclient=newWebClient();
              byte[]htmlData=null;
              htmlData=client.DownloadData(url);
              MemoryStreammstream=newMemoryStream(htmlData);
              stringhtml="";
              using(StreamReadersr=newStreamReader(mstream))
              {
                  html=sr.ReadToEnd();
              }
              Arrayurls=newMatchHtmlImageUrl().MatchHtml(html,com);

              foreach(stringimageurlinurls)
              {
                 Console.WriteLine(imageurl);
                  byte[]imageData=null;
                  try
                  {
                      imageData=client.DownloadData(imageurl);
                  }
                  catch{}
                  if(imageData!=null&&imageData.Length>0)
                      using(MemoryStreamms=newMemoryStream(imageData))
                      {
                          try
                          {

                              stringext=Aping.Utility.File.FileOpration.ExtendName(imageurl);
                              ImageFormatformat=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;
                              }
                              Imageimage=newBitmap(ms);
                              if(Directory.Exists(path))
                                  image.Save(path+"\\"+cursor+ext,format);
                          }
                          catch(Exceptionex){Console.WriteLine(ex.Message);}
                      }
                  cursor++;
              }
              mstream.Close();
              File.WriteAllText(countFile,cursor.ToString(),Encoding.UTF8);
              Console.WriteLine("takedone...imagecount:"+(cursor-oldCount).ToString());
          }           
      }