C#多線程爬蟲抓取免費(fèi)代理IP的示例代碼
這里用到一個(gè)HTML解析輔助類:HtmlAgilityPack,如果沒有網(wǎng)上找一個(gè)增加到庫里,這個(gè)插件有很多版本,如果你開發(fā)環(huán)境是使用VS2005就2.0的類庫,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)頁方法
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頁
for (int i = 1; i <= 15; i++)
{
ThreadPool.QueueUserWorkItem(getProxyList, i);
}
Console.Read();
}
}
以上就是本文的全部內(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)頁的實(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#實(shí)現(xiàn)在匿名方法中捕獲外部變量的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在匿名方法中捕獲外部變量的方法,本文直接給出代碼實(shí)例,然后分析了代碼中的一些知識(shí)點(diǎn),需要的朋友可以參考下2015-03-03
C#統(tǒng)計(jì)C、C++及C#程序代碼行數(shù)的方法
這篇文章主要介紹了C#統(tǒng)計(jì)C、C++及C#程序代碼行數(shù)的方法,較為詳細(xì)的分析了C#統(tǒng)計(jì)文本文件的原理與相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Winform控件優(yōu)化Paint事件實(shí)現(xiàn)圓角組件及提取繪制圓角的方法
這篇文章主要介紹了Winform控件優(yōu)化Paint事件實(shí)現(xiàn)圓角組件及提取繪制圓角的方法,Windows?11下所有控件已經(jīng)默認(rèn)采用圓角,其效果更好、相對(duì)有著更好的優(yōu)化,只是這是默認(rèn)的行為,無法進(jìn)一步自定義,更多詳情需要的小伙伴可以參考下面文章內(nèi)容2022-08-08
C#?wpf定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定
綁定機(jī)制是wpf的核心,也是界面獨(dú)立的根本,尤其是使用了mvvm模式,本文主要介紹了wpf如何定義ViewModelBase進(jìn)行簡(jiǎn)化屬性綁定,需要的可以參考下2024-04-04
C#實(shí)現(xiàn)Access通用訪問類OleDbHelper完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)Access通用訪問類OleDbHelper,結(jié)合完整實(shí)例形式分析了C#針對(duì)access數(shù)據(jù)庫的連接、查詢、遍歷、分頁顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-02-02

