pyppeteer攔截器使用


# 設置request攔截器
await page.setRequestInterception(True) # 設置request攔截器,想對請求作修改需要加這句
page.on('request',lambda req: asyncio.ensure_future(intercept_request(req)))
# 設置response攔截器
page.on('response',lambda rep: asyncio.ensure_future(intercept_response(rep)))

# 請求攔截器函數,設置攔截條件並可作修改
async def intercept_request(interceptedRequest):
   if 'xxx' in interceptedRequest.url:
       await interceptedRequest.continue_({"url": "xxx"}) # 修改url為xxx
   else:
     await
interceptedRequest.continue_()

# 響應攔截器函數,設置攔截條件並可作修改
async def intercept_response(interceptedResponse):
   if 'xxx' in
interceptedResponse.url:
       response = await interceptedResponse.text() # 獲得請求的text內容

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM