代码如下:
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);//获取域名
& nbsp; 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());
}
}
您可能感兴趣的文章:C#通过正则表达式实现提取网页中的图片提取HTML代码中文字的C#函数提取HTML代码中文字的C#函数c#在excel中添加超链接示例分享C#提取网页中超链接link和text部分的方法
汇编语言常见错误信息中文注解
C语言kmp算法简单示例和实现原理探究
上述就是C#学习教程:c#实现网页图片提取工具代码分享分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ctvol.com/cdevelopment/905132.html