我正在用Node和Cheerio构建一个网页刮板,对于某个网站,我得到以下错误(它只发生在这一个网站上,没有其他我试图刮的网站。

它每次都发生在不同的位置,所以有时是url x抛出错误,其他时候url x是好的,它是一个完全不同的url:

    Error!: Error: socket hang up using [insert random URL, it's different every time]

Error: socket hang up
    at createHangUpError (http.js:1445:15)
    at Socket.socketOnEnd [as onend] (http.js:1541:23)
    at Socket.g (events.js:175:14)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

这是非常棘手的调试,我真的不知道从哪里开始。首先,什么是套接字挂起错误?是404错误还是类似的错误?或者仅仅意味着服务器拒绝连接?

我在任何地方都找不到解释!

编辑:下面是(有时)返回错误的代码示例:

function scrapeNexts(url, oncomplete) {
    request(url, function(err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        $ = cheerio.load(body);
        // do stuff with the '$' cheerio content here
    });
}

没有直接调用关闭连接,但我使用节点请求(据我所知)使用http。get所以这是不需要的,如果我错了纠正我!

编辑2:下面是一段实际使用的代码,它会导致错误。prodURL和其他变量主要是前面定义的jquery选择器。这使用了Node的异步库。

function scrapeNexts(url, oncomplete) {
    request(url, function (err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        async.series([
                function (callback) {
                    $ = cheerio.load(body);
                    callback();
                },
                function (callback) {
                    $(prodURL).each(function () {
                        var theHref = $(this).attr('href');
                        urls.push(baseURL + theHref);
                    });
                    var next = $(next_select).first().attr('href');
                    oncomplete(next);
                }
            ]);
    });
}

当前回答

下面是一个简单的例子,当我错过添加下面的例子注释代码时,我得到了相同的错误。取消注释req.end()代码将解决此问题。

var fs = require("fs");
var https = require("https");

var options = {
    host: "en.wikipedia.org",
    path: "/wiki/George_Washington",
    port: 443,
    method: "GET"
};

var req = https.request(options, function (res) {
    console.log(res.statusCode);
});


// req.end();

其他回答

昨天通过IntelliJ IDEA 2016.3.6运行我的web应用程序和node.js服务器时遇到了这个问题。我所要做的就是清除我的cookie并在Chrome浏览器中缓存。

我认为“套接字挂起”是一个相当普遍的错误,表明连接已从服务器端终止。换句话说,用于维护客户端和服务器之间连接的套接字已经断开。(虽然我确信上面提到的许多观点对不同的人都有帮助,但我认为这是更普遍的答案。)

In my case, I was sending a request with a payload in excess of 20K. This was rejected by the server. I verified this by removing text and retrying until the request succeeded. After determining the maximum acceptable length, I verified that adding a single character caused the error to manifest. I also confirmed that the client wasn't the issue by sending the same request from a Python app and from Postman. So anyway, I'm confident that, in my case, the length of the payload was my specific problem.

同样,问题的根源是坊间传闻。常见的问题是“服务器说不”。

我同时做web(节点)和Android开发,并一起打开Android Studio设备模拟器和docker,它们都使用端口8601,它抱怨套接字挂起错误,关闭Android Studio设备模拟器后,它在节点端工作良好。不要同时使用Android Studio设备模拟器和docker。

这里似乎还有一个额外的情况,那就是Electron不喜欢“localhost”域名。在我的情况下,我需要改变这个:

const backendApiHostUrl = "http://localhost:3000";

:

const backendApiHostUrl = "http://127.0.0.1:3000";

从那以后,问题就消失了。

这意味着DNS解析(本地或远程)也可能导致一些问题。

您的问题也可能来自试图连接到HTTP URL,而您的服务仅在HTTPS上发布…

绝对是一个耗时的错误!