昨天写了一篇关于“c#批量抓取免费代理并验证有效性”的文章,接着昨天的目标继续完成吧,最终实现的目的就是刷新csdn文章的浏览量(实际上很简单,之前博客园的文章也是可以使用代理ip来刷的,后来不行了),刷文章的浏览量本身是可耻的,没有任何意义,当然技术无罪。之前是在csdn写文章,自从csdn改版之后就主要在博客园写。
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字典集合来统计失败的次数,如果达到一定次数就直接从库中删除该代理。关于代码中主要功能是实现,不足的地方就是代理太少,效率不高。
效果如图:
昨天晚上看了篇文章,故事很强大,警惕挂着开源的招牌到处坑蒙拐骗的垃圾项目,比如iBase4J,所以就找到原博主在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(, );
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 = ;
if (proxyIps.Count > )
{
threadCount = proxyIps.Count / ;
}
//平均分配给每个thread执行15个请求
int requestCount = proxyIps.Count / threadCount;
for (var i = ; i < threadCount; i++)
{
var tempList = proxyIps.GetRange(i * requestCount, requestCount);
if (i == threadCount - )
{
tempList.AddRange(proxyIps.GetRange(threadCount * requestCount, proxyIps.Count - threadCount * requestCount));
}
Thread thread = new Thread(async () =>
{
//执行发起请求
await Finish(tempList);
});
thread.Start();
}
}
}, "processing timer event", , *);
Console.ReadLine();
} public static async Task Finish(List<string> proxyIps)
{
for (int i = ; i < proxyIps.Count; i++)
{
string ip = proxyIps[i];
int index = ip.IndexOf(":");
string ipAddress = ip.Substring(, index);
int ipPort = int.Parse(ip.Substring(index + ));
//随机休眠
Thread.Sleep(new Random().Next(,)*);
await Get(ipAddress, ipPort, , 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] :)+"次");
if (failStatis.ContainsKey(ip))
{
if (failStatis[ip] == )
{
RedisHelper.RemoveSetValue(ip);
}
else
failStatis[ip]++;
}
else
{
failStatis.AddOrUpdate(ip, ,(key,oldValue)=>oldValue+);
}
}
);
}
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(, 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
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