您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C#爬蟲如何通過代理刷文章瀏覽量”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C#爬蟲如何通過代理刷文章瀏覽量”這篇文章吧。
1.如何維護代理IP庫?
想要使用代理IP,那就必須有一個一定數量、足夠有效的代理ip庫,在學習階段,隨便玩玩那就只能從免費的代理IP網站上抓取,沒有一定數量的代理刷文章流浪量非常慢,首先就是要維護好自己的代理Ip庫
之前用過的西刺代理、66ip比較靠譜,西祠好像有反扒,遇到過一次,不知道是西祠網站的問題還是反扒的策略,這兩個網站每分鐘抓取的能用的代理大概有2,3個,這已經算的上比較客觀的了, data5u、快代理、ip3366網頁端更新的非常少,而且有效性比較低,快代理抓取網頁還必須設置Useragent,發現設置后獲取的ip的端口和網頁端不一致,很玩味是吧,沒辦法免費的就是這樣,不然人家就收費了,當然付費的代理也不穩定,但肯定是比免費的好很多。
維護代理質量
從網頁端抓取下來的代理,肯定是要經過驗證再入庫的,最簡單的方式就是發起一個請求狀態碼是否為200。我推薦的免費代理還是上面兩個西刺代理和66ip,相對其他的免費代理有效性、數量都比較高。
代理如何儲存
我使用的是redis來存儲這些有效代理,數據結構最好是采用Set,不允許存儲相同的IP。代理的有效性無法得知,有的可能是幾十秒鐘,有的幾十分鐘,在使用的時候應該記錄那些多次不能使用的IP,達到一定次數,應該從Set中刪除掉。無法確定代理的時效,代理ip要及時使用,可以使用定時器定時從redis中取出代理。
2.反爬蟲的一些常見的機制?
反爬蟲的原則就是判斷是否是一個真實的用戶,一些比較重要的數據會利用多種機制混合,讓爬蟲的代價變大甚至無法爬取,Header里面的字段設置、IP限制、Cookie等
IP限制
一些網站為了防止爬蟲,可能會對每個ip進行訪問頻率的限制,訪問頻率一個是速度,可以同Thread.Sleep來進行休眠,暫停一會兒再進行爬取;一個IP次數這個我們可以通過抓取的免費代理來設置。
Header里的限制
User-Agent :用戶代理,這個非常簡單,可以收集一些常見的瀏覽器代理頭,在請求的時候隨機設置User-Agent
Referer :訪問目標鏈接是從哪個鏈接條過來的,做防圖片的盜鏈可以用它來處理,當然這個Refresh也是可以偽造。
Cookie:登錄后或其他的一些用戶操作,服務端會返回一些cookie信息,沒有cookie很容易被識別為偽造請求,可以在本地通過Js,根據服務端返回的一些信息,本地區設置cooke,當然這個實際中并沒有這么簡單,一般會涉及到加密解密的過程。這個是爬蟲的一個難點。
3.使用代理IP刷新csdn文章的瀏覽量
csdn文章的瀏覽量還是比較好刷的,前提的是你有足夠多的代理,沒有更多的代理效率會非常慢。前面一篇文章我們已經從幾個免費的代理網站抓取了代理,這里就不多做介紹了,這里我們接著上一篇的拿來就用。c#批量抓取免費代理并驗證有效性。1.我使用的多線程批量發送請求,效率更好,每個線程平均分配一定數量的代理執行請求。2.定時獲取Redis中代理3.使用System.Collections.Concurrent 命名空間下ConcurrentDictionary字典集合來統計失敗的次數,如果達到一定次數就直接從庫中刪除該代理。關于代碼中主要功能是實現,不足的地方就是代理太少,效率不高。
效果如圖:
昨天晚上看了篇文章,(這里是隨便CSDN或者其他地方的有瀏覽量的文章),刷的時間不短,主要是因為代理太少了。
主要代碼如下:
class Program { static bool finishIsCompleted=true; static ConcurrentDictionary<string, int> failStatis;//保存請求失敗的ip地址:key 失敗次數:value static string refreshLink = "https://blog.csdn.net/baijifeilong/article/details/80734388"; static string requestSuccessKey,requestFailKey; static async Task Main(string[] args) { ThreadPool.SetMinThreads(500, 100); failStatis = new ConcurrentDictionary<string, int>(); requestSuccessKey = "list_request_success"+DateTime.Now.ToString("hhmm"); requestFailKey = "list_request_fail" + DateTime.Now.ToString("hhmm"); Timer timer = new Timer(async (state) => { if (finishIsCompleted) { finishIsCompleted = false; //獲取代理 var proxyIps = RedisHelper.GetProxy(); int threadCount = 1; if (proxyIps.Count > 10) { threadCount = proxyIps.Count / 10; } //平均分配給每個thread執行15個請求 int requestCount = proxyIps.Count / threadCount; for (var i = 0; i < threadCount; i++) { var tempList = proxyIps.GetRange(i * requestCount, requestCount); if (i == threadCount - 1) { tempList.AddRange(proxyIps.GetRange(threadCount * requestCount, proxyIps.Count - threadCount * requestCount)); } Thread thread = new Thread(async () => { //執行發起請求 await Finish(tempList); }); thread.Start(); } } }, "processing timer event", 0, 1000*30); Console.ReadLine(); } public static async Task Finish(List<string> proxyIps) { for (int i = 0; i < proxyIps.Count; i++) { string ip = proxyIps[i]; int index = ip.IndexOf(":"); string ipAddress = ip.Substring(0, index); int ipPort = int.Parse(ip.Substring(index + 1)); //隨機休眠 Thread.Sleep(new Random().Next(1,4)*1000); await Get(ipAddress, ipPort, 10000, randomUseragent(), refreshLink, () => { RedisHelper.AddRequestOk(requestSuccessKey,ip+" "+DateTime.Now.ToShortTimeString(),true); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(ip+" success"); }, (error) => { RedisHelper.AddRequestOk(requestFailKey, ip + " " + DateTime.Now.ToShortTimeString(),false); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ipAddress+error+"失敗"+(failStatis.ContainsKey(ip)?failStatis[ip] :1)+"次"); if (failStatis.ContainsKey(ip)) { if (failStatis[ip] == 6) { RedisHelper.RemoveSetValue(ip); } else failStatis[ip]++; } else { failStatis.AddOrUpdate(ip, 1,(key,oldValue)=>oldValue+1); } } ); } finishIsCompleted = true; } private static string randomUseragent() { string[] usersagents = new string[] { "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1", "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10", "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7", "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10", "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+", "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0", "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124", "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36", "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10", "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER) ", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36", "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+", "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)", "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999", "Openwave/ UCWEB7.0.2.37/28/999", "NOKIA5700/ UCWEB7.0.2.37/28/999", "UCWEB7.0.2.37/28/999", "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0", "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13", "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10", "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", }; Random random = new Random(); var randomNumber = random.Next(0, usersagents.Length); return usersagents[randomNumber]; } public static async Task Get(string proxyIp, int proxyPort,int timeout, string randomUseragent, string url, Action success, Action<string> fail) { HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = timeout; request.UserAgent = randomUseragent; request.Proxy = new WebProxy(proxyIp,proxyPort); response = await request.GetResponseAsync() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) { success(); } else { fail(response+":"+response.StatusDescription); } } catch (Exception ex) { fail(ex.Message.ToString()); } finally { if (request != null) { request.Abort(); request = null; } if (response != null) { response.Close(); response = null; } } } }
redisHelper.cs
public class RedisHelper { private static readonly object Locker = new object(); private static ConnectionMultiplexer _redis; private const string CONNECTTIONSTRING = "127.0.0.1:6379,DefaultDatabase=3"; public const string REDIS_SET_KET_SUCCESS = "set_success_ip"; private static ConnectionMultiplexer Manager { get { if (_redis == null) { lock (Locker) { if (_redis != null) return _redis; _redis = GetManager(); return _redis; } } return _redis; } } private static ConnectionMultiplexer GetManager(string connectionString = null) { if (string.IsNullOrEmpty(connectionString)) { connectionString = CONNECTTIONSTRING; } return ConnectionMultiplexer.Connect(connectionString); } public static void AddRequestOk(string key,string value,bool isSuccess) { var db = Manager.GetDatabase(); if(isSuccess) db.ListLeftPush(key,value); else db.ListLeftPush(key, value); } public static List<string> GetProxy() { List<string> result = new List<string>(); var db = Manager.GetDatabase(); var values = db.SetMembers(REDIS_SET_KET_SUCCESS); foreach (var value in values) { result.Add(value.ToString()); } return result; } public static bool InsertSet(string value) { var db = Manager.GetDatabase(); return db.SetAdd(REDIS_SET_KET_SUCCESS, value); } public static bool RemoveSetValue(string value) { var db = Manager.GetDatabase(); return db.SetRemove(REDIS_SET_KET_SUCCESS,value); } }
以上是“C#爬蟲如何通過代理刷文章瀏覽量”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。