c#實(shí)現(xiàn)網(wǎng)頁(yè)圖片提取工具代碼分享
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();
}
[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 + "\\" + 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)文章
C#使用CancellationTokenSource 取消 Task的方法
因?yàn)樯婕暗搅巳粘=?jīng)常會(huì)碰到的取消任務(wù)操作,本文主要介紹了C#使用CancellationTokenSource 取消 Task,文中通過(guò)代碼介紹的非常詳細(xì),感興趣的可以了解一下2022-02-02
C#實(shí)現(xiàn)WinForm全屏置頂?shù)氖纠a
我們?cè)谶\(yùn)行一些?Windows?應(yīng)用程序的時(shí)候,需要將其運(yùn)行在窗體置頂?shù)哪J?并且進(jìn)入全屏狀態(tài),本文將介紹如何使用?C#?來(lái)實(shí)現(xiàn)?WinForm?的全屏置頂?shù)幕竟δ?感興趣的可以了解下2024-12-12
C#實(shí)現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將商品金額小寫轉(zhuǎn)換成大寫的方法,涉及C#數(shù)組與字符串的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#創(chuàng)建簡(jiǎn)單windows窗體應(yīng)用(加法器)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建一個(gè)簡(jiǎn)單windows窗體應(yīng)用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子
這篇文章介紹了在c#中把字符串轉(zhuǎn)為變量名并獲取變量值的小例子,有需要的朋友可以參考一下2013-09-09
c#斐波那契數(shù)列(Fibonacci)(遞歸,非遞歸)實(shí)現(xiàn)代碼
c#斐波那契數(shù)列(Fibonacci)(遞歸,非遞歸)實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05

