zoukankan      html  css  js  c++  java
  • Scrapy爬虫(八):中间件的使用实例

    Scrapy爬虫(八):中间件的使用实例

    本章将介绍最简单的中间件user-agent中间件的使用。

    user-agent

    user-agent是咱们模拟浏览器比较重要的参数,主要是防止爬虫被ban,前几章我们了解到在settings.py中可以设置user-agent,如:

    USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'

    但是这种情况还是有被禁(ban)的情况,另外我们再增加延时爬取,也可以减小被ban的风险,当然这些都是比较简单的伪装技巧,作为爬虫入门已经够用。
    那么我们设置更多的user-agent来模拟浏览器下载网页数据,每次下载时随机设置一个user-agent,这样更不容易被ban。

    最简单的中间件

    新建一个文件middlewares.py

    # -*-coding:utf-8-*-
    
    
    import random
    from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware
    
    
    class RotateUserAgentMiddleware(UserAgentMiddleware):
        def __init__(self, user_agent=''):
            self.user_agent = user_agent
    
        def process_request(self, request, spider):
            ua = random.choice(self.user_agent_list)
            if ua:
                # print(ua)
                request.headers.setdefault('User-Agent', ua)
    
        # the default user_agent_list composes chrome,I E,firefox,Mozilla,opera,netscape
        # for more user agent strings,you can find it in http://www.useragentstring.com/pages/useragentstring.php
        user_agent_list = [ 
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1" 
            "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", 
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", 
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", 
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", 
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", 
            "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", 
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", 
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", 
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", 
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", 
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
        ]

    settings.py中配置中间件

    DOWNLOADER_MIDDLEWARES = {
        'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
        'douban.middlewares.RotateUserAgentMiddleware': 543,
    }

    这样我们就设置了一个最简单的中间件到爬虫中,每次爬取的时候都会从RotateUserAgentMiddleware中随机设置一个user-agent,本章实例以增加到上章的实例中。

    github地址


  • 相关阅读:
    【HDOJ】1558 Segment set
    【HDOJ】2395 Alarm Clock
    【HDOJ】4278 Faulty Odomete
    【HDOJ】2571 命运
    【HDOJ】2150 Pipe
    【HDOJ】1277 全文检索
    【HDOJ】1134 Game of Connections
    【HDOJ】5131 Song Jiang's rank list
    【HDOJ】2772 Matchsticks
    【HDOJ】2577 How to Type
  • 原文地址:https://www.cnblogs.com/grimm/p/13884659.html
Copyright © 2011-2022 走看看