内容简介:在官方文档(这样一开始也是没有什么问题,但是偶尔会遇到这样情况:
在官方文档( 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!',
});
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python高性能(第2版)
[加] 加布丽埃勒•拉纳诺(Gabriele Lanaro) / 袁国忠 / 人民邮电出版社 / 2018-8 / 59.00元
本书是一本Python性能提升指南,展示了如何利用Python的原生库以及丰富的第三方库来构建健壮的应用程序。书中阐释了如何利用各种剖析器来找出Python应用程序的性能瓶颈,并应用正确的算法和高效的数据结构来解决它们;介绍了如何有效地利用NumPy、Pandas和Cython高性能地执行数值计算;解释了异步编程的相关概念,以及如何利用响应式编程实现响应式应用程序;概述了并行编程的概念,并论述了如......一起来看看 《Python高性能(第2版)》 这本书的介绍吧!