欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng) C#抓取網(wǎng)頁(yè)Html源碼

 更新時(shí)間:2016年03月24日 17:10:32   作者:小蝦Joe  
這篇文章主要為大家詳細(xì)介紹了基于C#實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)的相關(guān)資料,即C#抓取網(wǎng)頁(yè)Html源碼,感興趣的小伙伴們可以參考一下

最近剛完成一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng),開(kāi)始的時(shí)候很迷茫,不知道如何入手,后來(lái)發(fā)現(xiàn)了很多的資料,不過(guò)真正能達(dá)到我需要,有用的資料--代碼很難找。所以我想發(fā)這篇文章讓一些要做這個(gè)功能的朋友少走一些彎路。

首先是抓取Html源碼,并選擇<ul class="post_list">  </ul>節(jié)點(diǎn)的href:要添加using System.IO;using System.Net;

private void Search(string url)
{
 string rl;
 WebRequest Request = WebRequest.Create(url.Trim());
 
 WebResponse Response = Request.GetResponse();
 
 Stream resStream = Response.GetResponseStream();
 
 StreamReader sr = new StreamReader(resStream, Encoding.Default);
 StringBuilder sb = new StringBuilder();
 while ((rl = sr.ReadLine()) != null)
 {
  sb.Append(rl);
 }
 
 
 string str = sb.ToString().ToLower();
 
 string str_get = mid(str, "<ul class=\"post_list\">", "</ul>");
 
 
 int start = 0;
 while (true)
 {
  if (str_get == null)
   break;
  string strResult = mid(str_get, "href=\"", "\"", out start);
  if (strResult == null)
   break;
  else
  {
   lab[url] += strResult;
   str_get = str_get.Substring(start);
  }
 }
}
 
 
 
 
private string mid(string istr, string startString, string endString)
{
 int iBodyStart = istr.IndexOf(startString, 0);    //開(kāi)始位置
 if (iBodyStart == -1)
  return null;
 iBodyStart += startString.Length;       //第一次字符位置起的長(zhǎng)度
 int iBodyEnd = istr.IndexOf(endString, iBodyStart);   //第二次字符在第一次字符位置起的首次位置
 if (iBodyEnd == -1)
  return null;
 iBodyEnd += endString.Length;        //第二次字符位置起的長(zhǎng)度
 string strResult = istr.Substring(iBodyStart, iBodyEnd - iBodyStart - 1);
 return strResult;
}
 
 
private string mid(string istr, string startString, string endString, out int iBodyEnd)
{
 //初始化out參數(shù),否則不能return
 iBodyEnd = 0;
 
 int iBodyStart = istr.IndexOf(startString, 0);    //開(kāi)始位置
 if (iBodyStart == -1)
  return null;
 iBodyStart += startString.Length;       //第一次字符位置起的長(zhǎng)度
 iBodyEnd = istr.IndexOf(endString, iBodyStart);   //第二次字符在第一次字符位置起的首次位置
 if (iBodyEnd == -1)
  return null;
 iBodyEnd += endString.Length;        //第二次字符位置起的長(zhǎng)度
 string strResult = istr.Substring(iBodyStart, iBodyEnd - iBodyStart - 1);
 return strResult;
}

好了,上面就是全部代碼了,如果你想要運(yùn)行出來(lái)的話,有些細(xì)節(jié)要自己修改下。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論