C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼
這里用到一個(gè)HTML解析輔助類:HtmlAgilityPack,如果沒有網(wǎng)上找一個(gè)增加到庫(kù)里,這個(gè)插件有很多版本,如果你開發(fā)環(huán)境是使用VS2005就2.0的類庫(kù),VS2010就使用4.0,以此類推..........然后直接創(chuàng)建一個(gè)控制臺(tái)應(yīng)用,將我下面的代碼COPY替換就可以運(yùn)行,下面就來講講我兩年前做爬蟲經(jīng)歷,當(dāng)時(shí)是給一家公司做,也是用的C#,不過當(dāng)時(shí)遇到一個(gè)頭痛的問題就是抓的圖片有病毒,然后系統(tǒng)掛了幾次。所以抓網(wǎ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中第一個(gè)是能用的代理標(biāo)題,所以這里從第二行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,表示這個(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)
{
//多線程同時(shí)抓15頁(yè)
for (int i = 1; i <= 15; i++)
{
ThreadPool.QueueUserWorkItem(getProxyList, i);
}
Console.Read();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- c# Selenium爬取數(shù)據(jù)時(shí)防止webdriver封爬蟲的方法
- C# 爬蟲簡(jiǎn)單教程
- 用C#做網(wǎng)絡(luò)爬蟲的步驟教學(xué)
- c#爬蟲爬取京東的商品信息
- C#程序如何調(diào)用C++?dll詳細(xì)教程
- C# 利用代理爬蟲網(wǎng)頁(yè)的實(shí)現(xiàn)方法
- 利用C#實(shí)現(xiàn)最基本的小說爬蟲示例代碼
- C#簡(jiǎn)單爬蟲案例分享
- C#制作多線程處理強(qiáng)化版網(wǎng)絡(luò)爬蟲
- 利用C#實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲
- c# 基于Titanium爬取微信公眾號(hào)歷史文章列表
相關(guān)文章
c# winform讀取xml文件創(chuàng)建菜單的代碼
動(dòng)態(tài)創(chuàng)建菜單使得程序靈活性大大增加,本文根據(jù)讀取xml文件中的配置菜單項(xiàng)來動(dòng)態(tài)創(chuàng)建菜單,代碼如下2013-09-09
WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法
這篇文章主要介紹了WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
C#中使用IFormattable實(shí)現(xiàn)自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實(shí)現(xiàn)自定義格式字符串輸出示例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06
SuperSocket入門--Telnet服務(wù)器和客戶端請(qǐng)求處理
本文的控制臺(tái)項(xiàng)目是根據(jù)SuperSocket官方Telnet示例代碼進(jìn)行調(diào)試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01
c#高效的線程安全隊(duì)列ConcurrentQueue<T>的實(shí)現(xiàn)
這篇文章主要介紹了c#高效的線程安全隊(duì)列ConcurrentQueue<T>的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

