Node JS https 请求套接字挂断(mikeal/request 模块)



我是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' 
}

OP 这里

我所做的只是添加:

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+ 小时我再也回不来了

最新更新