C#多線程爬蟲抓取免費代理IP的示例代碼
這里用到一個HTML解析輔助類:HtmlAgilityPack,如果沒有網上找一個增加到庫里,這個插件有很多版本,如果你開發(fā)環(huán)境是使用VS2005就2.0的類庫,VS2010就使用4.0,以此類推..........然后直接創(chuàng)建一個控制臺應用,將我下面的代碼COPY替換就可以運行,下面就來講講我兩年前做爬蟲經歷,當時是給一家公司做,也是用的C#,不過當時遇到一個頭痛的問題就是抓的圖片有病毒,然后系統(tǒng)掛了幾次。所以抓網站圖片要注意安全,雖然我這里沒涉及到圖片,但是還是提醒下看文章的朋友。
class Program { //存放所有抓取的代理 public static List<proxy> masterPorxyList = new List<proxy>(); //代理IP類 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中第一個是能用的代理標題,所以這里從第二行TR開始取值 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,表示這個代理速度快。 if (subSpeed > 90) { proxy temp = new proxy(ip, port, subSpeed); masterPorxyList.Add(temp); Console.WriteLine("當前是第:" + masterPorxyList.Count.ToString() + "個代理IP"); } } } } //抓網頁方法 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) { //多線程同時抓15頁 for (int i = 1; i <= 15; i++) { ThreadPool.QueueUserWorkItem(getProxyList, i); } Console.Read(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
c# winform讀取xml文件創(chuàng)建菜單的代碼
動態(tài)創(chuàng)建菜單使得程序靈活性大大增加,本文根據讀取xml文件中的配置菜單項來動態(tài)創(chuàng)建菜單,代碼如下2013-09-09C#中使用IFormattable實現自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實現自定義格式字符串輸出示例,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06SuperSocket入門--Telnet服務器和客戶端請求處理
本文的控制臺項目是根據SuperSocket官方Telnet示例代碼進行調試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01c#高效的線程安全隊列ConcurrentQueue<T>的實現
這篇文章主要介紹了c#高效的線程安全隊列ConcurrentQueue<T>的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11