Extract website images and Images

Source: Internet
Author: User

Extract website images and Images

Protected void sub_Click (object sender, EventArgs e)
{
Int I = 0;
String url = this. url. Text;
Directory. CreateDirectory ("D:/aspnetjia/" + url );
String result = GetWebRequest (url );
// This. content. InnerText = result;
MatchCollection results = Regex. Matches (result, @ "] * src = ([" "'] *) (? <Src> [^ '"] *) \ 1 [^>] *>", RegexOptions. IgnoreCase );
String [] d = new string [results. Count];
Foreach (Match m in results)
{
D [I] = m. Groups ["src"]. Value;
I ++;
}
For (int j = 0; j <d. Length; j ++)
{
String jieguo = "";
Int jj = j + 1;
String jie = d [j]. ToString ();
// If (jie. Contains (url ))
//{
// Jie. Replace (url ,"");
//}
If (jie. Contains ("www.") | jie. Contains ("http "))
{
Jieguo = "<a href = '" + jie + "'> </a> ";
}
Else
{
Jieguo = "<a href = '" + url + "/" + jie + "'> </a> ";
}
Random rand = new Random (); // defines a Random number, to prevent you from uploading images with duplicate names
String [] strSpil = jie. Split ('.'); // separate the address with A. Number (img/1.jpg)
String strEnd = strSpil [strSpil. length-1]. toLower (); // obtain the following ("jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF ", "BMP", "PNG", "JPEG ")

String strName = DateTime. Now. ToString ("yyyymmmddhhss") + rand. Next (100,999 9). ToString (); // get a different name.
String strPointEnd = strName + "." + strEnd;

System. Net. WebClient myWebClient = new System. Net. WebClient ();
If (jie. Contains ("http "))
{
MyWebClient. DownloadFile (jie, "D:/aspnetjia/" + url + "/" + strPointEnd );
}
Else
{
MyWebClient. DownloadFile (url + "/" + jie, "D:/aspnetjia/" + url + "/" + strPointEnd );
}
// Response. Write ("downloading" + j + "...");
}
Response. Write ("Please check the aspnetjia folder on your d disk ");
}
Private string GetWebRequest (string url)
{
Uri uri = new Uri (url );
WebRequest myReq = WebRequest. Create (uri );
WebResponse result = myReq. GetResponse ();
Stream receviceStream = result. GetResponseStream ();
StreamReader readerOfStream = new StreamReader (receviceStream, System. Text. Encoding. GetEncoding ("UTF-8 "));
String strHTML = readerOfStream. ReadToEnd ();
ReaderOfStream. Close ();
ReceviceStream. Close ();
Result. Close ();
Return strHTML;
}

Reprinted from: http://www.aspnetjia.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.