【问题标题】:Web crawling in javascript Error: Error: connect ETIMEDOUTjavascript中的Web爬网错误:错误:连接ETIMEDOUT
【发布时间】:2018-06-22 03:30:45
【问题描述】:

嗨,我遇到了这个错误,我正在关注如何使用 javascript 进行网络抓取的教程。但是当我执行它时我得到了这个错误

   Visiting page https://arstechnica.com/
                                                             testcrawl.js:6
Error: Error: connect ETIMEDOUT 50.31.169.131:443
                                                             testcrawl.js:9
TypeError: Cannot read property 'statusCode' of undefined
                                                             testcrawl.js:12

    at Request._callback (c:\Users\nab\practise\testcrawl.js:12:43)
    at self.callback (c:\Users\nab\node_modules\request\request.js:185:22)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at Request.onRequestError (c:\Users\nab\node_modules\request\request.js:877:8)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)

这些是我正在运行的脚本

var request = require('request');
var cheerio = require('cheerio');
var URL = require('url-parse');

var pageToVisit = "https://arstechnica.com/";
console.log("Visiting page " + pageToVisit);
request(pageToVisit, function(error, response, body) {
   if(error) {
     console.log("Error: " + error);
   }
   // Check status code (200 is HTTP OK)
   console.log("Status code: " + response.statusCode);
   if(response.statusCode === 200) {                      
     // Parse the document body
     var $ = cheerio.load(body);
     console.log("Page title:  " + $('title').text());
   }
});

为什么会出现这个错误,我该如何解决这个问题?

【问题讨论】:

  • 这个网站:https://arstechnica.com/ 正在从您的浏览器打开吗?
  • @InusSaha 是的,我从我的浏览器打开它
  • 您可以尝试设置timeout 选项看看吗?喜欢request({url:pageToVisit, timeout:20000}, function(error, response, body) {
  • @InusSaha 你的意思是这样的请求({url:'arstechnica.com', timeout:20000}, function(error, response, body) { console.log('error:', error) ; console.log('statusCode:', response && response.statusCode); console.log('body:', body); });
  • @nabskim 你的代码对我有用。

标签: javascript node.js npm web-crawler


【解决方案1】:

请尝试以下方法并告诉我结果。

var pageToVisit = "https://arstechnica.com/";
console.log("Visiting page " + pageToVisit);
request({url:pageToVisit,timeout:20000}, function(error, response, body) {
   if(error) {
     console.log("Error: " + error);
   }
   // Check status code (200 is HTTP OK)
   console.log("Status code: " + response.statusCode);
   if(response.statusCode === 200) {                      
     // Parse the document body
     var $ = cheerio.load(body);
     console.log("Page title:  " + $('title').text());
   }
});

请注意,我已添加 timeout 以查看进展情况。

【讨论】:

  • 嘿,我认为问题与代理有关...我尝试使用自己的数据进行连接,它工作正常...谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
相关资源
最近更新 更多