def process_request(self, request, spider): # Called for each request that goes through the downloader # middleware. # 每个交给下载器的request对象都会经过该方法,并期望返回response # Must either: # 如果该方法返回的是None,则继续执行其他下载中间件的process_request方法送往下载器,直到合适的下载器函数被调用,该request被执行,返回response # - return None: continue processing this request # 如果该方法返回的是response,则终止当前流程,也终止继续调用其他process_request方法,将该response通过引擎返回给爬虫 # - or return a Response object # 如果该方法返回的是request,则终止当前流程,也终止继续调用其他process_request方法,将request返回给调度器,大多数情况是更换新的request请求 # - or return a Request object # 抛出IgnoreRequest异常, 该异常就会交个process_exception方法进行处理; 如果没有任何一个方法处理该异常 # 那么该请求就直接被忽略了, 也不会记录错误日志. # - or raise IgnoreRequest: process_exception() methods of # installed downloader middleware will be called return None def process_response(self, request, response, spider): # Called with the response returned from the downloader. # 当下载器完成http请求,返回响应给引擎的时候调用 # Must either; # 如果返回response,则继续执行,其他下载中间件也会处理该response,直至交给引擎再交给爬虫 # - return a Response object # 如果返回request,则中间件终止,该request返回引擎再给调度器 # - return a Request object # 抛出 IgnoreRequest 异常; 该请求就被忽略了且不做记录 # - or raise IgnoreRequest return response 原文:https://blog.csdn.net/Hepburn_li/article/details/81478885