1

我正在尝试从网站获取一些数据,但出现以下错误:

{ FetchError: maximum redirect reached at: <URL>
    at ClientRequest.<anonymous> (C:\Users\Michal\Desktop\argentina\node_modules\node-fetch\lib\index.js:1498:15)
    at ClientRequest.emit (events.js:198:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:556:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
    at TLSSocket.socketOnData (_http_client.js:442:20)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:94:17)
  message:
   'maximum redirect reached at: <URL>',
  type: 'max-redirect' }

代码是:

const fetch = require("node-fetch");
const asyncFuncAsYouAsked = async () => {
    try {
        const response = await fetch(URL);
        const myJson = await response.json();
        let data = JSON.stringify(myJson);
        fs.writeFileSync('data.json', data);
        console.log(data);
    } catch(e) {
        console.log("EROOOOR:");
        console.dir(e);
    } 
}
 asyncFuncAsYouAsked();

'最大重定向达到:'错误是什么意思,这个问题的解决方案是什么?

4

1 回答 1

1

显然,您请求的页面正在重定向到另一个页面。例如,也许您正在通过 http 协议进行请求,并且它正在重定向到 https。

redirect如果没有,也许您仍然可以通过调整和follow option-settings来获取您尝试获取的数据。就像是:

const fetch = require("node-fetch");
const asyncFuncAsYouAsked = async () => {
    try {
        let options = {};
        options.redirect = "follow";
        options.follow = 20;
        const response = await fetch(URL,options);
        const myJson = await response.json();
        let data = JSON.stringify(myJson);
        fs.writeFileSync('data.json', data);
        console.log(data);
    } catch(e) {
        console.log("EROOOOR:");
        console.dir(e);
    } 
}
 asyncFuncAsYouAsked();
于 2019-11-10T05:53:41.740 回答