談談反爬蟲“政策與對策”

Roninwz發表於2017-10-10

寫部落格,一部分是為了讓自己今後能快速地複習之前學過的知識,整理下思路;另一方面是幫助到其他也遇到類似問題的童鞋。但是寫部落格很難堅持下來,原因嘛,各種各樣。但說到底是沒有“共鳴”。

高山流水,難覓知音。

其實,建立起寫部落格的習慣,就是那些點點滴滴的小事:每天看著部落格的訪問量,點贊數增加;看到自己的文章被別人評論等等。


好了,廢話不多說。今天來談談關於刷瀏覽量的問題。雖然這遠遠的偏離了寫部落格的初衷,但是瞭解下這類問題還是不錯的,畢竟“技術並不犯法!”。

反(反)爬蟲機制

說到反爬蟲,不得不說下爬蟲了。其實這就是一個概念,爬蟲就是將手動完成的事情交給了程式碼去自動化的實現罷了。而反爬蟲就是探查使用者是真實使用者還是程式碼的一種手段。而反反爬蟲就是針對反爬蟲機制的一種手段。

都說“雙重否定,表示肯定”,那麼爬蟲和反反爬蟲應該是一樣的了。其實不然,表面上行為是一致的,但是實際上反反爬蟲做了更多的處理,而不是簡單的小爬蟲啦。

大體上來講,反爬蟲會從如下幾個層面入手: 
- header 瀏覽器的請求頭 
- User-Agent 使用者代理,表明訪問源身份的一種方式 
- Referer 訪問的目標連結是從哪個連結跳轉過來的(做防盜鏈的話,就可以從它入手) 
- Host 同源地址判斷,用它會很有用 
- IP 同一個IP短時多次訪問,就很有可能是爬蟲,反爬蟲會對此做處理。 
- 訪問頻率:短時多次高併發的訪問,基本上就是有問題的訪問。 
上面這幾個都是常見的反爬蟲措施,當然還有更加高深的機制,比如最噁心的驗證碼(使用tesseract可以處理較為簡單的驗證碼識別),使用者行為分析,等等等等。

既然瞭解了常見的反爬蟲機制,那相對應的進行“政策-對策”實現反反爬蟲也就不是那麼的沒有頭緒了。是的,針對上面的限制,我們會有一些對策。

  • 針對User-Agent 的,可以整理一些常見的瀏覽器代理頭,每次訪問隨機使用其中一個就好了。
  • 針對IP的,可以使用代理IP嘛
  • 針對頻率限制的,做下訪問間隙做下隨機休眠就挺不錯的。
  • ……

實戰

之前我一直是在CSDN上寫部落格,它的反爬蟲機制說實話,做的比較的淺,一方面必要性不是很大,二來做反爬蟲經紀上不太划算,估計他們也不願意在這上面浪費吧。

所以,在CSDN上刷瀏覽量還是很隨意的,說下我的思路。 
- 代理IP爬取,驗證清洗資料,定期更新。 
- 瀏覽器User-Agent整理,新增訪問的隨機性。 
- 隨即休眠策略,日誌處理,錯誤記錄,定時重試等。

代理IP處理

# coding: utf8

# @Author: 郭 璞
# @File: proxyip.py                                                                 
# @Time: 2017/10/5                                   
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 抓取代理IP,並儲存到redis相關的key中
import requests
from bs4 import BeautifulSoup
from redishelper import RedisHelper

class ProxyIP(object):
    """
    抓取代理IP,清洗,驗證。
    """
    def __init__(self):
        self.rh = RedisHelper()

    def crawl(self):
        """
        不管是http還是https統統存進去再說。
        """
        # 先處理http模式的代理ip
        httpurl = "http://www.xicidaili.com/nn/"
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
        }
        html = requests.get(url=httpurl, headers=headers).text
        soup = BeautifulSoup(html, "html.parser")
        ips = soup.find_all("tr")
        for index in range(1, len(ips)):
            tds = ips[index].find_all('td')
            ip = tds[1].text
            port = tds[2].text
            ipinfo = "{}:{}".format(ip, port)
            if self._check(ip):
                self.rh.sAddAvalibeIp(ipinfo)
            # print(ipinfo)




    def _check(self, ip):
        """
        檢測代理IP的有效性
        """
        checkurl = "http://47.94.19.186/common/checkip.php"
        localip = self._getLocalIp()
        # print("Local: {}, proxy: {}".format(localip, ip))
        return False if localip==ip else True

    def _getLocalIp(self):
        """
        獲取本機的IP地址, 介面方式不太靠譜,暫時用手工方式在https://www.baidu.com/s?ie=UTF-8&wd=ip 進行手動複製貼上即可
        """
        return "223.91.239.159"

    def clean(self):
        ips = self.rh.sGetAllAvalibleIps()
        for ipinfo in ips:
            ip, port = ipinfo.split(":")
            if self._check(ip):
                self.rh.sAddAvalibeIp(ipinfo)
            else:
                self.rh.sRemoveAvalibleIp(ipinfo)

    def update(self):
        pass


if __name__ == '__main__':
    pip = ProxyIP()
    # result = pip._check("223.91.239.159", 53281)
    # print(result)
    pip.crawl()
    # pip.clean()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

Redis工具類

# coding: utf8

# @Author: 郭 璞
# @File: redishelper.py                                                                 
# @Time: 2017/10/5                                   
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 涉及redis的一些操作工具方法

import redis

class RedisHelper(object):
    """
    用於儲存爬取到的部落格內容連結。
    儲存代理IP
    """
    def __init__(self):
        self.articlepool = "redis:set:article:pool"
        self.avalibleips = "redis:set:avalible:ips"
        self.unavalibleips = "redis:set:unavalibe:ips"

        pool = redis.ConnectionPool(host="localhost", port=6379)
        self.redispool = redis.Redis(connection_pool=pool)

    def sAddArticleId(self, articleid):
        """
        新增爬取到的部落格id。
        :param articleid:
        :return:
        """
        self.redispool.sadd(self.articlepool, articleid)

    def sRemoveArticleId(self, articleid):
        self.redispool.srem(self.articlepool, articleid)

    def popupArticleId(self):
        return int(self.redispool.srandmember(self.articlepool))

    def sAddAvalibeIp(self, ip):
        self.redispool.sadd(self.avalibleips, ip)
    def sRemoveAvalibeIp(self, ip):
        self.redispool.srem(self.avalibleips, ip)
    def sGetAllAvalibleIps(self):
        return [ip.decode('utf8') for ip in self.redispool.smembers(self.avalibleips)]

    def popupAvalibeIp(self):
        return self.redispool.srandmember(self.avalibleips)

    def sAddUnavalibeIp(self, ip):
        self.redispool.sadd(self.unavalibleips, ip)
    def sRemoveUnavaibleIp(self, ip):
        self.redispool.srem(self.unavalibleips, ip)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

csdn博文工具類

# coding: utf8

# @Author: 郭 璞
# @File: csdn.py                                                                 
# @Time: 2017/10/5                                   
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 爬取一個博主的全部部落格連結工具類以及其他設計到的操作。
import re
import requests
from bs4 import BeautifulSoup

class BlogScanner(object):
    """
    抓取博主id下的所有文章連結id。
    """
    def __init__(self, bloger="marksinoberg"):
        self.bloger = bloger
        # self.blogpagelink = "http://blog.csdn.net/{}/article/list/{}".format(self.bloger, 1)

    def _getTotalPages(self):
        blogpagelink = "http://blog.csdn.net/{}/article/list/{}?viewmode=contents".format(self.bloger, 1)
        html = requests.get(url=blogpagelink).text
        soup = BeautifulSoup(html, "html.parser")
        # 比較hack的操作,實際開發還是不要這麼隨意的好
        temptext = soup.find('div', {"class": "pagelist"}).find("span").get_text()
        restr = re.findall(re.compile("(\d+).*?(\d+)"), temptext)
        # print(restr)
        pages = restr[0][-1]
        return pages

    def _parsePage(self, pagenumber):
        blogpagelink = "http://blog.csdn.net/{}/article/list/{}?viewmode=contents".format(self.bloger, int(pagenumber))
        html = requests.get(url=blogpagelink).text
        soup = BeautifulSoup(html, "html.parser")
        links = soup.find("div", {"id": "article_list"}).find_all("span", {"class": "link_title"})
        articleids = []
        for link in links:
            temp = link.find("a").attrs['href']
            articleids.append(temp.split("/")[-1])
        # print(len(articleids))
        # print(articleids)
        return articleids

    def get_all_articleids(self):
        pages = int(self._getTotalPages())
        articleids = []
        for index in range(pages):
            tempids = self._parsePage(int(index+1))
            articleids.extend(tempids)
        return articleids



if __name__ == '__main__':
    bs = BlogScanner(bloger="marksinoberg")
    # print(bs._getTotalPages())
    # bs._parsePage(1)
    articleids = bs.get_all_articleids()
    print(len(articleids))
    print(articleids)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

Brush工具類

# coding: utf8

# @Author: 郭 璞
# @File: brushhelper.py                                                                 
# @Time: 2017/10/5                                   
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 開刷

import requests
import random
import time
from redishelper import RedisHelper


class FakeUserAgent(object):
    """
    蒐集到的一些User-Agent,每次popup出不同的ua,減少反爬蟲機制的影響。
    更多內容:http://www.73207.com/useragent
    """

    def __init__(self):
        self.uas = [
            "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",
        ]

    def _generateIndexes(self):
        numbers = random.randint(0, len(self.uas))
        indexes = []
        while len(indexes) < numbers:
            temp = random.randrange(0, len(self.uas))
            if temp not in indexes:
                indexes.append(temp)
        return indexes

    def popupUAs(self):
        uas = []
        indexes = self._generateIndexes()
        for index in indexes:
            uas.append(self.uas[index])
        return uas


class Brush(object):
    """
    開刷瀏覽量
    """

    def __init__(self, bloger="marksinoberg"):
        self.bloger = "http://blog.csdn.net/{}".format(bloger)
        self.headers = {
            'Host': 'blog.csdn.net',
            'Upgrade - Insecure - Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36',
        }
        self.rh = RedisHelper()

    def getRandProxyIp(self):
        ip = self.rh.popupAvalibeIp()
        proxyip = {}
        ipinfo = "http://{}".format(str(ip.decode('utf8')))
        proxyip['http'] = ipinfo
        # print(proxyip)
        return proxyip


    def brushLink(self, articleid, randuas=[]):
        # http://blog.csdn.net/marksinoberg/article/details/78058279
        bloglink = "{}/article/details/{}".format(self.bloger, articleid)
        for ua in randuas:
            self.headers['User-Agent'] = ua
            timeseed = random.randint(1, 3)
            print("臨時休眠: {}秒".format(timeseed))
            time.sleep(timeseed)
            for index in range(timeseed):
                # requests.get(url=bloglink, headers=self.headers, proxies=self.getRandProxyIp())
                requests.get(url=bloglink, headers=self.headers)


if __name__ == '__main__':
    # fua = FakeUserAgent()

    # indexes = [0, 2,5,7]
    # indexes = generate_random_numbers(0, 18, 7)
    # randuas = fua.popupUAs(indexes)
    # randuas = fua.popupUAs()
    # print(len(randuas))
    # print(randuas)

    # print(fua._generateIndexes())

    brush = Brush("marksinoberg")
    # brush.brushLink(78058279, randuas)
    print(brush.getRandProxyIp())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

入口

# coding: utf8

# @Author: 郭 璞
# @File: Main.py                                                                 
# @Time: 2017/10/5                                   
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 入口

from csdn import *
from redishelper import RedisHelper
from brushhelper import *
import threading

def main():
    rh = RedisHelper()
    bs = BlogScanner(bloger="marksinoberg")
    fua = FakeUserAgent()
    brush = Brush(bloger="marksinoberg")

    counter = 0
    while counter < 12:
        # 開刷
        print("第{}次!".format(counter))
        try:
            uas = fua.popupUAs()
            articleid = rh.popupArticleId()
            brush.brushLink(articleid, uas)
        except Exception as e:
            print(e)
            # 待新增日誌處理程式
        counter+=1


if __name__ == '__main__':
    for i in range(280):
        temp = threading.Thread(target=main)
        temp.start()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

執行結果

我拿了之前寫過的一篇文章做了下測試。 
博文連結:http://blog.csdn.net/marksinoberg/article/details/78058279

開刷之前為301個瀏覽量,簡單刷了下之後,訪問量為下圖:

簡單刷了下瀏覽量之後


總結

大致就是這個樣子啦,雖然這頂多算個原型,因為程式碼完成度45%左右。有興趣的可以加我QQ1064319632, 或者在評論中留下您的建議,大家一起交流,一起學習。


轉載來自:http://blog.csdn.net/marksinoberg/article/details/78168223

相關文章