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
