usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Threading.Tasks;namespacemeizi{ Public classProgram {Static voidMain (string[] args) { NewGetmeizipic (); } } /// <summary> ///Get sister Pictures/// </summary> Public classGetmeizipic {Private ReadOnly string_path; Private Const stringImgregex =@"]*?src\s*=\s*["'"? ([^ ' "" ">]+?) [ ‘""] [^>]*?>";//Regular Expressions for pictures Private Const stringLinkregex =@""; PublicGetmeizipic () {_path= Dealdir (Path.Combine (Environment.currentdirectory,"Images")); Console.WriteLine ("=============== started collecting ==============="); for(vari =1; I <Ten; i++) {Console.WriteLine ("=============== downloading the {0} page data ===============", i); DOFETCHSTEP1 (i); } Console.WriteLine ("=============== Acquisition Complete ==============="); } Private stringDealdir (stringpath) { if(!directory.exists (path)) Directory.CreateDirectory (path); returnpath; } Private voidDOFETCHSTEP1 (intpagenum) { varRequest = (HttpWebRequest) webrequest.create ("http://www.sepaidui.com/?sort=4&page="+pagenum); Request. Credentials=CredentialCache.DefaultCredentials; varResponse =(HttpWebResponse) request. GetResponse (); if(Response. StatusCode! = Httpstatuscode.ok)return; varstream =Response. GetResponseStream (); if(Stream = =NULL)return; using(varSR =NewStreamReader (Stream)) {FetchLinksFromSource1 (Sr. ReadToEnd ()); } } Private voidFetchLinksFromSource1 (stringHtmlsource) { varMatcheslink = Regex.Matches (Htmlsource, Linkregex, Regexoptions.ignorecase |regexoptions.singleline); foreach(Match minchMatcheslink) { stringhref = m.groups[0]. Value.split ('"')[1]; DOFETCHSTEP2 (HREF); } } Private voidDOFETCHSTEP2 (stringhref) { varRequest =(HttpWebRequest) webrequest.create (HREF); varh =request. Headers; Request. Credentials=CredentialCache.DefaultCredentials; varResponse =(HttpWebResponse) request. GetResponse (); if(Response. StatusCode! = Httpstatuscode.ok)return; varstream =Response. GetResponseStream (); if(Stream = =NULL)return; using(varSR =NewStreamReader (Stream)) {FetchLinksFromSource2 (Sr. ReadToEnd ()); } } Private voidFetchLinksFromSource2 (stringHtmlsource) { varMATCHESIMGSRC = Regex.Matches (Htmlsource, Imgregex, Regexoptions.ignorecase |regexoptions.singleline); foreach(Match minchmatchesimgsrc) { varhref = m.groups[1]. Value; //select only pictures from Sina albums if(href.) Contains ("sinaimg") && checkisurlformat (href) &&!href. Contains ("60d02b59tw1eq6g7srmiwj20pv03mdg8") {Console.WriteLine (href); } Else Continue; using(varMywebclient =NewWebClient ()) { Try{mywebclient.downloadfile (NewUri (HREF), Path.Combine (_path, path.getrandomfilename () +path.getextension (HREF))); } Catch(Exception ex) {Console.WriteLine (ex). Message); } } } } Private ReadOnlyRegex _isurlformat =NewRegex (@"http//? ([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?"); Private BOOLCheckisurlformat (stringvalue) { return_isurlformat.ismatch (value); } }}
C # programs