日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决

發布時間:2023/12/20 ChatGpt 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在官方文檔(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();elseinterceptedRequest.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!', });

總結

以上是生活随笔為你收集整理的puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。