網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
下載中間件
在每一個(gè)scrapy工程中都有一個(gè)名為 middlewares.py 的文件,這個(gè)就是中間件文件
其中下載中間件的類為 XxxDownloaderMiddleware
其中有這么幾個(gè)方法
def process_request(self, request, spider): return None
def process_response(self, request, response, spider): return response
def process_exception(self, request, exception, spider): pass
process_request
這個(gè)方法是用來(lái)攔截請(qǐng)求的,我們可以將UA偽裝寫(xiě)在這個(gè)方法中。
UA池這個(gè)屬性需要自己編寫(xiě)
def process_request(self, request, spider): # UA偽裝,從UA池隨機(jī)一個(gè) request.headers['User-Agent'] = random.choice(self.user_agent_list) return None
process_response
這個(gè)方法是用來(lái)攔截響應(yīng)的,我們可以在這里篡改響應(yīng)數(shù)據(jù)。
如果我們將selenium和scrapy結(jié)合就可以請(qǐng)求那些動(dòng)態(tài)加載的數(shù)據(jù)了。
def process_response(self, request, response, spider): # 瀏覽器對(duì)象 bro = spider.bro # 參數(shù)spider是爬蟲(chóng)對(duì)象 # 挑選出指定響應(yīng)對(duì)象進(jìn)行篡改url->request->response bro.get(request.url) page_text = bro.page_source # 包含了動(dòng)態(tài)加載的數(shù)據(jù) # 針對(duì)定位到的response篡改 # 實(shí)例化新的響應(yīng)對(duì)象(包含動(dòng)態(tài)加載的數(shù)據(jù)) response = HtmlResponse(url=bro.current_url, body=page_text, encoding='utf-8', request=request) return response
在爬蟲(chóng)文件中需要預(yù)先創(chuàng)建selenium的瀏覽器對(duì)象
import scrapy from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver import ChromeOptions class XxxSpider(scrapy.Spider): name = 'xxx' # allowed_domains = ['www.xxx.com'] start_urls = ['……'] def __init__(self): service = Service('/Users/soutsukyou/PyCharm_Workspace/網(wǎng)絡(luò)爬蟲(chóng)/study_selenium/chromedriver') chrome_options = ChromeOptions() # 規(guī)避檢測(cè) chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 實(shí)例化瀏覽器 self.bro = webdriver.Chrome(service=service, options=chrome_options)
process_exception
這是用來(lái)攔截發(fā)生異常的請(qǐng)求對(duì)象,一般我們可以在這里寫(xiě)代理ip。
兩個(gè)代理ip池屬性需要自己編寫(xiě)
def process_exception(self, request, exception, spider): # 可以設(shè)置代理ip if request.url.split(':')[0] == 'http': request.meta['proxy'] = 'http://'+random.choice(self.PROXY_http) if request.url.split(':')[0] == 'https': request.meta['proxy'] = 'https://'+random.choice(self.PROXY_https) # 重新請(qǐng)求發(fā)送 return request
其它
我們需要在settings.py中開(kāi)啟下載中間件才能使其生效
# Enable or disable downloader middlewares # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html DOWNLOADER_MIDDLEWARES = { 'xxx.middlewares.XxxDownloaderMiddleware': 543, }
原文鏈接:https://www.cnblogs.com/S2Jgogo/p/16052157.html
相關(guān)推薦
- 2024-04-03 clickhouse報(bào)Ports are not available
- 2022-05-01 Python中的datetime包與time包包和模塊詳情_(kāi)python
- 2023-01-12 Golang單元測(cè)試與斷言編寫(xiě)流程詳解_Golang
- 2022-09-10 Python自動(dòng)打印被調(diào)用函數(shù)變量名及對(duì)應(yīng)值?_python
- 2022-02-25 Oracle?觸發(fā)器實(shí)現(xiàn)主鍵自增效果_oracle
- 2022-09-17 Go語(yǔ)言學(xué)習(xí)筆記之錯(cuò)誤和異常詳解_Golang
- 2022-11-08 background-image 背景平鋪方式、 CSS3 background-size背景圖像大
- 2022-09-14 Python使用pip安裝Matplotlib的方法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支