pyppeteer拦截器使用
# 设置request拦截器
await page.setRequestInterception(True)
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内容