内容简介:在官方文档(这样一开始也是没有什么问题,但是偶尔会遇到这样情况:
在官方文档( puppeteer/api.md at master · GoogleChrome/puppeteer · GitHub )中,中断 redirect 的标准做法是这样的:
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
if (interceptedRequest.url().endsWith('.png') || interceptedRequest.url().endsWith('.jpg'))
interceptedRequest.abort();
else
interceptedRequest.continue();
});
await page.goto('https://example.com');
await browser.close();
});
这样一开始也是没有什么问题,但是偶尔会遇到这样情况:
Error: net::ERR_FAILED at http://xxx.com/yyy
Google 了一轮,发现相关的 issue 很少,只找到了这么一个:
Page.setRequestInterception Redirection Issue · Issue #3421 · GoogleChrome/puppeteer · GitHub官方已经把它定义为一个 Bug 了,也有一些相关的解决方案: umbrella Fix Request Interception · Issue #3471 · GoogleChrome/puppeteer · GitHub
不过其他人遇到的情况是 abort() 之后无法结束的问题,而我是抛出异常的问题,所以我自己摸索了一下,总结出一个比较合适的办法:
就是用 respond 代替 abort。
比如:
// request.abort();
request.respond({
status: 404,
contentType: 'text/plain',
body: 'Not Found!',
});
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- React 折腾记 - (10) UmiJS 2.x + antd 重写后台管理系统记录的问题及解决姿势
- 强大的姿势感知模型用于姿势不变的人脸识别
- 从姿势到图像——基于人体姿势引导的时尚图像生成算法
- 行人重识别告别辅助姿势信息,港中文、商汤等提出姿势无关的特征提取GAN
- 穿越边界的姿势
- 日志打印的正确姿势!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming From The Ground Up
Jonathan Bartlett / Bartlett Publishing / 2004-07-31 / USD 34.95
Programming from the Ground Up is an introduction to programming using assembly language on the Linux platform for x86 machines. It is a great book for novices who are just learning to program as wel......一起来看看 《Programming From The Ground Up》 这本书的介绍吧!