[C#]網路扒蟲

來源:互聯網
上載者:User

這個其實不能算是完全意義上的網路扒蟲,只是對某個社交網路進行扒取,然後得到鄰接矩陣,以及相應的頭像等資訊。

 

 

 

主要的步驟:

1,扒取資訊

2,正則匹配

 

 

Regex主要參考了:http://deerchao.net/tutorials/regex/regex.htm

 

扒取資訊中用的是WebClient這個方法相對HttpRequest的HttpResponse更簡潔一些。

 

痛點是克服網站的認證機制,用的是儲存Cookies的方法。

 

扒虫部分代碼:

usingSystem;

usingSystem.Net;

usingSystem.Text;

usingSystem.Text.RegularExpressions;

 

publicclassCrawler

{

   publicstaticstringGetCont(stringurl)//扒取頁面

   {

     stringcookies="_r01_=;depovince=BJ;p=;ap=;t=;societyguester=55a777838c4286ab5f657382dbd25c736;id=;xnsid=";

     WebClientWebC=newWebClient();

     WebC.Headers.Add("Cookie",cookies);

     byte[]WebPa=WebC.DownloadData(url);

     stringPageHtml=Encoding.UTF8.GetString(WebPa);

     returnPageHtml;

   }

   publicstaticvoidGetImag(stringImgUrl,stringUserName)//下載小圖片

   { 

     stringimageFileName;

     stringimageFilePath;

     WebClientmyClient=newWebClient();

     Regexregex=newRegex("//w*");

     MatchCollectionUsNameMatches=regex.Matches(UserName);

     imageFileName=UsNameMatches[0].Value.ToString()+".jpg";

     imageFilePath=@"D:/picture/"+imageFileName;

     try

     {

       myClient.DownloadFile(ImgUrl,imageFilePath);

     }

     catch

     {

     }

   }

}

 

//Regex部分

usingSystem.Text;

usingSystem.Text.RegularExpressions;

 

publicclassMyRegex

{

   publicstaticstring[]GetAddr(stringPageHtml)

   {

     string[]PageUrl=newstring[24];

     Regexregex=newRegex("http://www..com/profile.do//?portal=//w*&id=//d+(?=/"//stitle=)");

     MatchCollectionurlMatches=regex.Matches(PageHtml);

 

     for(inti=0;i<urlMatches.Count;i++)

     {

       PageUrl[i]=urlMatches[i].Value.ToString();

     }

     returnPageUrl;

   }

   publicstaticstring[]GetImgAddr(stringPageHtml)

   {

     string[]ImgAddr=newstring[24];

     Regexregex=newRegex("(?<=stats=/"pf_friend/"//ssrc//=/").*(?=/"//swidth=/"50/"//s/>)");

     MatchCollectionImgMatches=regex.Matches(PageHtml);

     for(inti=0;i<ImgMatches.Count;i++)

     {

       ImgAddr[i]=ImgMatches[i].Value.ToString();

     }

     returnImgAddr;

   }

   publicstaticstring[]GetUsName(stringPageHtml)

   {

     string[]UsName=newstring[24];

     Regexregex=newRegex("(?<=title=/"查看).*(?=的個人首頁/">//W<img//sstats=/"pf_friend/")");

     MatchCollectionUsNameMatches=regex.Matches(PageHtml);

     for(inti=0;i<UsNameMatches.Count;i++)

     {

       UsName[i]=UsNameMatches[i].Value.ToString();

     }

     returnUsName;

   }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.