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

标签 node.js express dom fetch

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

Failed to fetch page: { FetchError: request to https://www1.nseindia.com/marketinfo/companyTracker/compInfo.jsp?symbol=TCS&series=EQ failed, reason: read ECONNRESET at ClientRequest

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

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);
    });
});

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

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

最佳答案

我会使用一些基于现代 promise 的包来完成这项工作。有些是 gotaxios 等。node-fetch 上次发布是在 8 个月前。它可能无法处理编码或压缩。

这里是一个使用 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);
  });

关于node.js - 在 Node.js 中获取 URL 时出现 ECONNRESET 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591058/

相关文章:

node.js - 使用 node.js 将文件上传到 Firebase 存储中的文件夹中

node.js - 托管 NodeJS 应用程序

javascript - 如何检查 JavaScript 对象是否为 DOM 对象?

javascript - jQuery 错误 : Invalid object initializer

javascript - this 指向模块范围内的什么地方?

javascript - Node 开发不连续运行

node.js - 将数据传递给 View 时,node ejs 引用错误数据未在 eval 处定义

jquery - jQuery css() 方法是否适用于外部样式表?

node.js - 如何使用webpack和babel转换ES6服务器端Node文件?

javascript - 使用 Angular 引入 Node 时出现的问题或如何在不使用 Angular-cli 的情况下创建平均应用程序