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