【问题标题】:Node JS https request socket hang up (mikeal/request module)Node JS https 请求套接字挂断(mikeal/request 模块)
【发布时间】:2018-08-31 02:54:21
【问题描述】:

我是 Node JS (v.10.9.0) 的新手,想制作一个简单的网络抓取工具,用于获取此页面上玩家的统计数据和排名。不管我不能让它在这个网站上工作,我尝试了多种请求方法,包括 http.request 和 https.request 并且已经让每个方法都可以使用'http://www.google.com'。然而,对这个特定网站的每次尝试都会给我一个 301 错误或一个套接字挂断错误。 301 错误给我的位置是相同的链接,但末尾带有“/”并请求它会导致套接字挂起。我知道该站点运行在 443 端口上。某些站点是否只是阻止节点 js,为什么浏览器能够连接但不能连接这样的东西?

请不要将我链接到我看过的任何其他主题,但它们都没有帮助

var request = require('request');

var options = {
    method: "GET",
    uri: 'https://www.smashboards.com',
    rejectUnauthorized: false,
    port: '443'
};

request(options, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:'); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

错误:

error: { Error: socket hang up
    at createHangUpError (_http_client.js:322:15)
    at TLSSocket.socketOnEnd (_http_client.js:425:23)
    at TLSSocket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1085:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }

编辑:

将此添加到我的选项对象解决了我的问题

headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' 
    }

【问题讨论】:

  • 转到浏览器中的网络选项卡。将该站点加载到浏览器中,并准确检查从浏览器发送到服务器的内容。然后,将其准确添加到您的 node.js 应用程序中。包括所有标题,包括 cookie、用户代理等……一切。如果操作正确,服务器将无法判断您是浏览器还是 node.js 应用程序。
  • 从您的问题中删除修复。该修复程序属于您的答案。问题是为了问题。答案是为了答案。

标签: node.js sockets


【解决方案1】:

在这里

我所做的只是添加:

headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' 
}

到我的选项对象,它工作得很好。

新代码:

var request = require('request');

var options = {
    method: 'GET',
    uri: 'https://www.smashboards.com',
    rejectUnauthorized: false,
    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' 
    }
};
request(options, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:'); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

那超过 12 小时我再也回不来了

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 2019-03-06
    • 2014-12-25
    • 2020-06-17
    • 1970-01-01
    • 2013-11-16
    • 2018-01-29
    • 2019-03-23
    相关资源
    最近更新 更多