這里用到一個(gè)HTML解析輔助類(lèi):HtmlAgilityPack,如果沒(méi)有網(wǎng)上找一個(gè)增加到庫(kù)里,這個(gè)插件有很多版本,如果你開(kāi)發(fā)環(huán)境是使用VS2005就2.0的類(lèi)庫(kù),VS2010就使用4.0,以此類(lèi)推..........然后直接創(chuàng)建一個(gè)控制臺(tái)應(yīng)用,將我下面的代碼COPY替換就可以運(yùn)行,下面就來(lái)講講我兩年前做爬蟲(chóng)經(jīng)歷,當(dāng)時(shí)是給一家公司做,也是用的C#,不過(guò)當(dāng)時(shí)遇到一個(gè)頭痛的問(wèn)題就是抓的圖片有病毒,然后系統(tǒng)掛了幾次。所以抓網(wǎng)站圖片要注意安全,雖然我這里沒(méi)涉及到圖片,但是還是提醒下看文章的朋友。
 class Program  {    //存放所有抓取的代理    public static List<proxy> masterPorxyList = new List<proxy>();    //代理IP類(lèi)    public class proxy    {      public string ip;      public string port;      public int speed;      public proxy(string pip,string pport,int pspeed)            {        this.ip = pip;        this.port = pport;        this.speed = pspeed;       }    }    //抓去處理方法    static void getProxyList(object pageIndex)    {      string urlCombin = "http://www.xicidaili.com/wt/" + pageIndex.ToString();      string catchHtml = catchProxIpMethord(urlCombin, "UTF8");            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();      doc.LoadHtml(catchHtml);      HtmlNode table = doc.DocumentNode.SelectSingleNode("http://div[@id='wrapper']//div[@id='body']/table[1]");      HtmlNodeCollection collectiontrs = table.SelectNodes("./tr");                 for (int i = 0; i < collectiontrs.Count; i++)        {          HtmlAgilityPack.HtmlNode itemtr = collectiontrs[i];          HtmlNodeCollection collectiontds = itemtr.ChildNodes;          //table中第一個(gè)是能用的代理標(biāo)題,所以這里從第二行TR開(kāi)始取值          if (i>0)          {            HtmlNode itemtdip = (HtmlNode)collectiontds[3];            HtmlNode itemtdport = (HtmlNode)collectiontds[5];            HtmlNode itemtdspeed = (HtmlNode)collectiontds[13];             string ip = itemtdip.InnerText.Trim();            string port = itemtdport.InnerText.Trim();            string speed = itemtdspeed.InnerHtml;            int beginIndex = speed.IndexOf(":", 0, speed.Length);            int endIndex = speed.IndexOf("%", 0, speed.Length);            int subSpeed = int.Parse(speed.Substring(beginIndex + 1, endIndex - beginIndex - 1));            //如果速度展示條的值大于90,表示這個(gè)代理速度快。           if (subSpeed > 90)            {              proxy temp = new proxy(ip, port, subSpeed);                            masterPorxyList.Add(temp);              Console.WriteLine("當(dāng)前是第:" + masterPorxyList.Count.ToString() + "個(gè)代理IP");            }                     }        }     }    //抓網(wǎng)頁(yè)方法    static string catchProxIpMethord(string url,string encoding )    {      string htmlStr = "";      try      {        if (!String.IsNullOrEmpty(url))        {          WebRequest request = WebRequest.Create(url);            WebResponse response = request.GetResponse();                Stream datastream = response.GetResponseStream();           Encoding ec = Encoding.Default;          if (encoding == "UTF8")          {            ec = Encoding.UTF8;          }          else if (encoding == "Default")          {            ec = Encoding.Default;          }          StreamReader reader = new StreamReader(datastream, ec);          htmlStr = reader.ReadToEnd();                  reader.Close();          datastream.Close();          response.Close();        }      }      catch { }      return htmlStr;    }   static void Main(string[] args)     {       //多線(xiàn)程同時(shí)抓15頁(yè)       for (int i = 1; i <= 15; i++)       {                          ThreadPool.QueueUserWorkItem(getProxyList, i);       }       Console.Read();     }  }以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注