在 Node.js 中获取 URL 时出现 ECONNRESET 错误

JA0*_*007 5 dom fetch node.js express

尝试从以下链接获取(使用 npm node-fetch )html 时出现以下错误:

无法获取页面:{ FetchError:对 https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ的请求 失败,原因:在 ClientRequest 处读取 ECONNRESET

我正在使用以下代码片段:

const DomParser = require("dom-parser");
const parser = new DomParser();
const fetch = require("node-fetch");

router.get("/info", (req, res, next) => {
  var url =
    "https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ";
  fetch(url)
    .then(function(response) {
      // When the page is loaded convert it to text
      return response.text();
    })
    .then(function(html) {
      // Initialize the DOM parser

      // Parse the text
      var doc = parser.parseFromString(html, "text/html");

      // You can now even select part of that html as you would in the regular DOM
      // Example:
      // var docArticle = doc.querySelector('article').innerHTML;

      console.log(doc);
    })
    .catch(function(err) {
      console.log("Failed to fetch page: ", err);
    });
});

Run Code Online (Sandbox Code Playgroud)

在显示错误之前,响应被控制台记录了几次,现在每次我调用 /info 时都会抛出错误。

我已经在 repl 在线编辑器中尝试过该片段。它返回Promise {pending}

Ash*_*odi 1

我会使用一些基于承诺的现代包来完成这项工作。some aregot等最后发布于 8 个月前axiosnode-fetch它可能无法处理编码或压缩。

这是一个使用axios它的示例。

const axios = require("axios");
const DomParser = require("dom-parser");
const parser = new DomParser();

var url =
  "https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ";
axios(url)
  .then(response => response.data)
  .then(html => {
    // Initialize the DOM parser

    // Parse the text
    var doc = parser.parseFromString(html, "text/html");

    // You can now even select part of that html as you would in the regular DOM
    // Example:
    // var docArticle = doc.querySelector('article').innerHTML;

    console.log(doc);
  })
  .catch(function(err) {
    console.log("Failed to fetch page: ", err);
  });

Run Code Online (Sandbox Code Playgroud)