The first is two ways to read HTML. Two encoding formats, two different methods (which can be, the second garbled, pending)
Private Static stringreturnutf8html () {Try{WebClient mywebclient=NewWebClient (); Mywebclient.credentials= CredentialCache.DefaultCredentials;//Gets or sets the network credentials that are used to authenticate requests to Internet resourcesbyte[] Pagedata = Mywebclient.downloaddata ("http://www.bing.com");//download data from a specified Web site//string pagehtml = Encoding.Default.GetString (pagedata); //If you are using GB2312 to get a website page, use this sentence stringpagehtml = Encoding.UTF8.GetString (pagedata);//If you are using UTF-8 to get a website page, use this sentence//Console.WriteLine (pagehtml);//Enter what you get in the console using(StreamWriter SW =NewStreamWriter ("f:\\desktop\\txt2.html"))//writes the acquired content to the text{SW. Write (pagehtml); } returnpagehtml; } Catch(WebException webEx) {//Console.WriteLine (webEx.Message.ToString ()); returnwebEx.Message.ToString (); } }
Public Static stringreturngb2312html () {stringGetweatherurl ="http://www.ithome.com/"; //WebRequest webreq = WebRequest.Create (Getweatherurl); //WebResponse webresp = Webreq.getresponse (); //Stream stream = Webresp.getresponsestream ();Stream Request Byte =webrequest.create (Getweatherurl). GetResponse (). GetResponseStream (); StreamReader Getting content=NewStreamReader (Request Byte, encoding.getencoding ("gb2312")); stringHTML =gets the content. ReadToEnd (); Gets the content. Close (); The request byte. Close (); //Save Web pageFileStream FStream =NewFileStream (@"F:\Desktop\txt.html", FileMode.OpenOrCreate, FileAccess.Write); byte[] buffer =Encoding.Default.GetBytes (HTML); FStream. Write (Buffer,0, buffer. Length); FStream. Close (); FStream. Dispose (); returnhtml; }
Crawling files
Public Static stringCrawl Files (stringstrURL) { stringSTRMSG =string. Empty; Try{WebRequest Request=WebRequest.Create (strURL); WebResponse response=request. GetResponse (); Stream Reader=Response. GetResponseStream (); FileStream writer=NewFileStream (Program.path, FileMode.OpenOrCreate, FileAccess.Write); //FileStream writer = new FileStream (@ "F:\Desktop\Bing" +name+ ". jpg", FileMode.OpenOrCreate, FileAccess.Write); byte[] Buff =New byte[ +]; intc =0;//the number of bytes actually read while(c = reader.) Read (Buff,0, Buff. Length)) >0) {writer. Write (Buff,0, c); } writer. Close (); Writer. Dispose (); Reader. Close (); Reader. Dispose (); Response. Close (); STRMSG="saved successfully"; } Catch { } returnSTRMSG; }
Entrance
1 Static voidMain (string[] args)2 {3Console.WriteLine ("reading Web page ...");4 stringHtml=returnutf8html ();5Console.WriteLine ("Web page read Normal ...");6Console.WriteLine ("matching Regular expression ...");7 //Console.WriteLine (HTML);8 //String w = program.returngb2312html ();9 //Console.WriteLine (w);TenMatch Regular expression OneMatch match= regex.match (HTML,@"http://s.cn.bing.net/az/hprichbg/rb/[a-za-z]+_zh-cn[0-9]{11}_1920x1080.jpg"); A - Console.WriteLine (match. Value); - the Program . Fetch file (match. Value.tostring ()); -Console.WriteLine ("Download Complete! "); - -Thread.Sleep ( the); + //Console.readkey (); - //SystemParametersInfo (0, "d:\\aaa.bmp", 0x2); +}
Path
Path public static string path = "E:\\background\\bing\\bing" + DateTime.Now.ToString ("Yyyy-mm-dd hh:m:ss") + ". jpg" ;
Bing Search Background Image crawl