昨天寫了一篇關於「 c#批量抓取免費代理並驗證有效性」的文章,接着昨天的目標繼續完成吧,最終實現的目的就是刷新csdn文章的瀏覽量(實際上很簡單,以前博客園的文章也是可使用代理ip來刷的,後來不行了),刷文章的瀏覽量自己是可恥的,沒有任何意義,固然技術無罪。以前是在csdn寫文章,自從csdn改版以後就主要在博客園寫。
想要使用代理IP,那就必須有一個必定數量、足夠有效的代理ip庫,在學習階段,隨便玩玩那就只能從免費的代理IP網站上抓取,沒有必定數量的代理刷文章流浪量很是慢,首先就是要維護好本身的代理Ip庫php
以前用過的西刺代理、66ip比較靠譜,西祠好像有反扒,遇到過一次,不知道是西祠網站的問題仍是反扒的策略,這兩個網站每分鐘抓取的能用的代理大概有2,3個,這已經算的上比較客觀的了, data5u、快代理、ip3366網頁端更新的很是少,並且有效性比較低,快代理抓取網頁還必須設置Useragent,發現設置後獲取的ip的端口和網頁端不一致,很玩味是吧,沒辦法免費的就是這樣,否則人家就收費了,固然付費的代理也不穩定,但確定是比免費的好不少。html
從網頁端抓取下來的代理,確定是要通過驗證再入庫的,最簡單的方式就是發起一個請求狀態碼是否爲200。我推薦的免費代理仍是上面兩個西刺代理和66ip,相對其餘的免費代理有效性、數量都比較高。es6
我使用的是redis來存儲這些有效代理,數據結構最好是採用Set,不容許存儲相同的IP。代理的有效性沒法得知,有的多是幾十秒鐘,有的幾十分鐘,在使用的時候應該記錄那些屢次不能使用的IP,達到必定次數,應該從Set中刪除掉。沒法肯定代理的時效,代理ip要及時使用,可使用定時器定時從redis中取出代理。redis
反爬蟲的原則就是判斷是不是一個真實的用戶,一些比較重要的數據會利用多種機制混合,讓爬蟲的代價變大甚至沒法爬取,Header裏面的字段設置、IP限制、Cookie等c#
一些網站爲了防止爬蟲,可能會對每一個ip進行訪問頻率的限制,訪問頻率一個是速度,能夠同Thread.Sleep來進行休眠,暫停一下子再進行爬取;一個IP次數這個咱們能夠經過抓取的免費代理來設置。瀏覽器
User-Agent :用戶代理,這個很是簡單,能夠收集一些常見的瀏覽器代理頭,在請求的時候隨機設置User-Agentcookie
Referer :訪問目標連接是從哪一個連接條過來的,作防圖片的盜鏈能夠用它來處理,固然這個Refresh也是能夠僞造。數據結構
Cookie:登陸後或其餘的一些用戶操做,服務端會返回一些cookie信息,沒有cookie很容易被識別爲僞造請求,能夠在本地經過Js,根據服務端返回的一些信息,本地區設置cooke,固然這個實際中並無這麼簡單,通常會涉及到加密解密的過程。這個是爬蟲的一個難點。多線程
csdn文章的瀏覽量仍是比較好刷的,前提的是你有足夠多的代理,沒有更多的代理效率會很是慢。前面一篇文章咱們已經從幾個免費的代理網站抓取了代理,這裏就很少作介紹了,這裏咱們接着上一篇的拿來就用。c#批量抓取免費代理並驗證有效性。1.我使用的多線程批量發送請求,效率更好,每一個線程平均分配必定數量的代理執行請求。2.定時獲取Redis中代理3.使用System.Collections.Concurrent 命名空間下ConcurrentDictionary字典集合來統計失敗的次數,若是達到必定次數就直接從庫中刪除該代理。關於代碼中主要功能是實現,不足的地方就是代理太少,效率不高。dom
效果如圖:
昨天晚上看了篇文章,故事很強大,警戒掛着開源的招牌處處坑蒙拐騙的垃圾項目,好比iBase4J,因此就找到原博主在csdn的這篇文章曝光北京一家不發工資的流氓公司南充巴蜀文化,老闆叫萬明,刷的時間不短,主要是由於代理太少了。
主要代碼以下:
1 class Program 2 { 3 static bool finishIsCompleted=true; 4 static ConcurrentDictionary<string, int> failStatis;//保存請求失敗的ip地址:key 失敗次數:value 5 static string refreshLink = "https://blog.csdn.net/baijifeilong/article/details/80734388"; 6 static string requestSuccessKey,requestFailKey; 7 static async Task Main(string[] args) 8 { 9 10 ThreadPool.SetMinThreads(500, 100); 11 failStatis = new ConcurrentDictionary<string, int>(); 12 requestSuccessKey = "list_request_success"+DateTime.Now.ToString("hhmm"); 13 requestFailKey = "list_request_fail" + DateTime.Now.ToString("hhmm"); 14 Timer timer = new Timer(async (state) => 15 { 16 if (finishIsCompleted) 17 { 18 finishIsCompleted = false; 19 //獲取代理 20 var proxyIps = RedisHelper.GetProxy(); 21 int threadCount = 1; 22 if (proxyIps.Count > 10) 23 { 24 threadCount = proxyIps.Count / 10; 25 } 26 //平均分配給每一個thread執行15個請求 27 int requestCount = proxyIps.Count / threadCount; 28 for (var i = 0; i < threadCount; i++) 29 { 30 var tempList = proxyIps.GetRange(i * requestCount, requestCount); 31 if (i == threadCount - 1) 32 { 33 tempList.AddRange(proxyIps.GetRange(threadCount * requestCount, proxyIps.Count - threadCount * requestCount)); 34 } 35 Thread thread = new Thread(async () => 36 { 37 //執行發起請求 38 await Finish(tempList); 39 }); 40 thread.Start(); 41 } 42 } 43 }, "processing timer event", 0, 1000*30); 44 Console.ReadLine(); 45 } 46 47 public static async Task Finish(List<string> proxyIps) 48 { 49 for (int i = 0; i < proxyIps.Count; i++) 50 { 51 string ip = proxyIps[i]; 52 int index = ip.IndexOf(":"); 53 string ipAddress = ip.Substring(0, index); 54 int ipPort = int.Parse(ip.Substring(index + 1)); 55 //隨機休眠 56 Thread.Sleep(new Random().Next(1,4)*1000); 57 await Get(ipAddress, ipPort, 10000, randomUseragent(), refreshLink, () => 58 { 59 RedisHelper.AddRequestOk(requestSuccessKey,ip+" "+DateTime.Now.ToShortTimeString(),true); 60 Console.ForegroundColor = ConsoleColor.White; 61 Console.WriteLine(ip+" success"); 62 }, 63 (error) => 64 { 65 RedisHelper.AddRequestOk(requestFailKey, ip + " " + DateTime.Now.ToShortTimeString(),false); 66 Console.ForegroundColor = ConsoleColor.Red; 67 Console.WriteLine(ipAddress+error+"失敗"+(failStatis.ContainsKey(ip)?failStatis[ip] :1)+"次"); 68 if (failStatis.ContainsKey(ip)) 69 { 70 if (failStatis[ip] == 6) 71 { 72 RedisHelper.RemoveSetValue(ip); 73 } 74 else 75 failStatis[ip]++; 76 } 77 else 78 { 79 failStatis.AddOrUpdate(ip, 1,(key,oldValue)=>oldValue+1); 80 } 81 } 82 ); 83 } 84 finishIsCompleted = true; 85 } 86 87 private static string randomUseragent() 88 { 89 string[] usersagents = new string[] { 90 "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", 91 "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", 92 "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999", 93 "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1", 94 "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10", 95 "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", 96 "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", 97 "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", 98 "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", 99 "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+", 100 "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", 101 "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", 102 "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)", 103 "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", 104 "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", 105 "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", 106 "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50", 107 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", 108 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36", 109 "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) ", 110 "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)", 111 "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", 112 "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", 113 "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+", 114 "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)", 115 "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999", 116 "Openwave/ UCWEB7.0.2.37/28/999", 117 "NOKIA5700/ UCWEB7.0.2.37/28/999", 118 "UCWEB7.0.2.37/28/999", 119 "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", 120 "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", 121 "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10", 122 "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", 123 }; 124 Random random = new Random(); 125 var randomNumber = random.Next(0, usersagents.Length); 126 return usersagents[randomNumber]; 127 } 128 public static async Task Get(string proxyIp, int proxyPort,int timeout, string randomUseragent, string url, Action success, Action<string> fail) 129 { 130 HttpWebRequest request = null; 131 HttpWebResponse response = null; 132 try 133 { 134 request = (HttpWebRequest)WebRequest.Create(url); 135 request.Timeout = timeout; 136 request.UserAgent = randomUseragent; 137 request.Proxy = new WebProxy(proxyIp,proxyPort); 138 139 response = await request.GetResponseAsync() as HttpWebResponse; 140 141 if (response.StatusCode == HttpStatusCode.OK) 142 { 143 success(); 144 } 145 else 146 { 147 fail(response+":"+response.StatusDescription); 148 } 149 } 150 catch (Exception ex) 151 { 152 fail(ex.Message.ToString()); 153 } 154 finally 155 { 156 if (request != null) 157 { 158 request.Abort(); 159 request = null; 160 } 161 if (response != null) 162 { 163 response.Close(); 164 response = null; 165 } 166 } 167 } 168 }
redisHelper.cs
1 public class RedisHelper
2 { 3 private static readonly object Locker = new object(); 4 private static ConnectionMultiplexer _redis; 5 private const string CONNECTTIONSTRING = "127.0.0.1:6379,DefaultDatabase=3"; 6 public const string REDIS_SET_KET_SUCCESS = "set_success_ip"; 7 private static ConnectionMultiplexer Manager 8 { 9 get 10 { 11 if (_redis == null) 12 { 13 lock (Locker) 14 { 15 if (_redis != null) return _redis; 16 _redis = GetManager(); 17 return _redis; 18 } 19 } 20 return _redis; 21 } 22 } 24 private static ConnectionMultiplexer GetManager(string connectionString = null) 25 { 26 if (string.IsNullOrEmpty(connectionString)) 27 { 28 connectionString = CONNECTTIONSTRING; 29 } 30 return ConnectionMultiplexer.Connect(connectionString); 31 } 32 public static void AddRequestOk(string key,string value,bool isSuccess) 33 { 34 var db = Manager.GetDatabase(); 35 if(isSuccess) 36 db.ListLeftPush(key,value); 37 else 38 db.ListLeftPush(key, value); 39 } 40 public static List<string> GetProxy() 41 { 42 List<string> result = new List<string>(); 43 var db = Manager.GetDatabase(); 44 var values = db.SetMembers(REDIS_SET_KET_SUCCESS); 45 foreach (var value in values) 46 { 47 result.Add(value.ToString()); 48 } 49 return result; 50 } 51 public static bool InsertSet(string value) 52 { 53 var db = Manager.GetDatabase(); 54 return db.SetAdd(REDIS_SET_KET_SUCCESS, value); 55 } 56 public static bool RemoveSetValue(string value) 57 { 58 var db = Manager.GetDatabase(); 59 return db.SetRemove(REDIS_SET_KET_SUCCESS,value); 60 } 64 }
原文:C#使用代理刷新csdn文章瀏覽量
原文連接:https://www.cnblogs.com/zhangmumu/p/9275190.html
做者:張林
未經做者許可,可隨意轉載2018-07-06