蠻久以前寫的一個小程式,介紹給大家...
程式目的:下載網頁內含的JPG檔到C:\MY_PIC內
(當然你也可以透過IE右鍵另存圖片...哈哈)
程式原理:利用WebBrowser控制項,讀取網頁HTML Document,然後利用正則表達式,找出HTML內所有的圖檔路徑並下載.
正則表達式:
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
程式Layout說明:
以下列網頁為例:
http://dcview.com.tw/gallery/showmsg.asp?msgid=774265&place=1&posit=1
重要程式碼節錄
解析網頁
ArrayList FilesArrayList = new ArrayList();
MatchCollection FilesMachCollection = Regex.Matches(myWB.DocumentText, textBox2.Text, RegexOptions.Compiled);
for (int i = 0; i < FilesMachCollection.Count; i++)
{
FilesArrayList.Add(FilesMachCollection[i].ToString());
}
其中textBox2.Text = (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
下載圖檔
string myPath = @"C:\MY_PIC\";
using (WebClient client = new WebClient())
{
if (!Directory.Exists(myPath))
{
Directory.CreateDirectory(myPath);
a = 0;
}
else
{
a = Directory.GetFiles(myPath).Length + 1; //計算下一圖檔檔名序號
}
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
for (int i = 0; i < listBox1.Items.Count; i++)
{
Application.DoEvents();
string fileName = myPath + a.ToString().PadLeft(10, '0') + ".JPG";
client.DownloadFile(listBox1.Items[i].ToString(), fileName);
Application.DoEvents();
double nowValue = (100 * (i + 1)) / listBox1.Items.Count;
progressBar1.Value = (int)nowValue;
Application.DoEvents();
a++;
}
}
執行檔[MD5 : 3076129e886d503a20022ae7cabeda8e]
Source Code[MD5 : 03800d5e8d311eeb7bf48193fbb3c709]
0 個回應:
張貼留言