zoukankan      html  css  js  c++  java
  • 下载器中间件

    Downloader Middlewares(下载器中间件)

    下载器中间件是引擎和下载器之间通信的中间件。在这个中间件中我们可以设置代理、更换请求头等来达到反反爬虫的目的。要写下载器中间件,可以在下载器中实现两个方法。一个是process_request(self, spider),这个方法是在请求发送之前执行,还有一个是process_response(self, request, response, spider),这个方法是数据下载到引擎之前执行。

    process_request(self, request, spider)

    这个方法是下载器在发送请求之前会执行的。一般可以在这个里面设置随机代理ip等。

    1. 参数:
      • request:发送请求的request对象。
      • spider:发送请求的spider对象。
    2. 返回值:
      • 返回None:如果返回NoneScrapy将继续处理该request,执行其他中间件中的相应方法,直到合适的下载器处理函数被调用。
      • 返回Response对象:Scrapy将不会调用任何其他的process_request方法,将直接返回这个response对象。已经激活的中间件的process.response()方法则会在每个response返回时被调用。
      • 返回Request对象:不再使用之前的request对象去下载数据,而是根据现在返回的request对象返回数据。
      • 如果这个方法中抛出了异常,则会调用process_exception方法。

    process_response(self, request, response, spider)

    这个是下载器下载的数据到引擎中间会中心会执行的方法。

    1. 参数:
      • request:request对象。
      • response:被处理的response对象。
      • spider:spider对象。
    2. 返回值:
      • 返回Response对象:会将这个新的response对象传给其他中间件,最终传给爬虫。
      • 返回Request对象:下载链接切断,返回的request会重新被下载器调度下载。
      • 如果抛出一个异常,那么调用requesterrback方法,如果没有指定这个方法,那么会抛出一个异常。

    随机请求头中间件:

    爬虫在频繁访问一个页面的时候,这个请求头如果一直保持一致。那么很容易被服务器发现,从而禁止掉这个请求头的访问。因此我们要在访问这个页面之前随机的更改请求头,这样才可以避免爬虫被抓。随机更改请求头,可以在下载中间件中实现。在请求发送给服务器之前,随机的选择一个请求头。这样就可以避免总使用一个请求头了。示例代码如下:

    class UserAgentDownloadMiddleware(object):
        # user-agent随机请求头中间件
        USER_AGENT = [
            '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; U; PPC Mac OS X; pl-PL; rv:1.0.1) Gecko/20021111 Chimera/0.6',
            'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201',
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 SE 2.X MetaSr 1.0',
        ]
        
        def process_request(self, request, spider):
            user_agent = random.choice(self.USER_AGENT)
            request.headers['User-Agent'] = user_agent
    

    user-agent列表:http://www.useragentstring.com/pages/useragentstring.php?typ=Browser

    ip代理池中间件

    购买代理:
    在以下代理商中购买代理:

    1. 芝麻代理:http://http.zhimaruanjian.com
    2. 太阳代理:http://http.taiyangruanjian.com
    3. 快代理:http://www.kuaidaili.com
    4. 讯代理:http://www.xdaili.com
    5. 蚂蚁代理:http://www.mayidaili.com

    使用代理池:
    示例代码如下:

    class IPProxyDownloadMiddleware(object):
        POXIES = [
            "178.44.170.152:8080",
            "110.44.113.182:8080",
            "209.126.124.73:8888"
        ]
        def process_request(self, request, spider):
            proxy = random.choice(self.PROXIES)
            request.meta['proxy'] = proxy
    
    1. 独享代理池设置:
    class IPProxyDownloadMiddleware(object):
        def process_request(self, request, spider)
            proxy = '121.199.6.124:16816'
            user_password = '970138074:rcdj35ur'
            request.meta['proxy'] = proxy
            # byte
            b64_user_passwd = base64.b64encode(user_password.encode('utf-8'))
            request.headers['proxy-Authorization'] = 'Basic' + b64_user_password.decode('utf-8')
    
  • 相关阅读:
    rabbitmq队列
    什么是RPC
    图解rabbitmq
    rabbitmq的面试题
    nginx介绍
    nginx正向代理和反向代理
    负载均衡 分布式 集群
    Nginx的负载均衡
    缓存雪崩 穿透 击穿
    Python Day32:UDP协议、UDP与TCP协议的区别、以及其服务端、客户端标准代码
  • 原文地址:https://www.cnblogs.com/colden/p/9863815.html
Copyright © 2011-2022 走看看