2015-11-30 49 views
0

我想从下面的网址抓取当前的德里温度。我正在使用MEAN堆栈。我不太了解jQuery。有时我得到错误:无法抓取正确的数据

{ [Error: socket hang up] code: 'ECONNRESET' } 

有时我得到整个一周的温度。如何获得那个温度?帮我

api.get('/scrape', function (req, res) { 



     url = "http://www.timeanddate.com/weather/india/new-delhi"; 
     request(url,function(error,response,html) { 
      if (!error) { 
       var $ = cheerio.load(html); 
       var title, release, rating; 
       var json = {title: "", release: "", rating: ""}; 
       $('.').filter(function() { 
        var data = $(this); 
        title = data.children().first().text(); 
        release = data.children().last().children().text(); 
        json.title = title; 
        json.release = release; 
       }); 

       console.log('before'); 

       var data=$('div.rows').find('span'); 

       console.log(data.text()); 

       console.log('after'); 
      } 
      else 
      { 
       console.log(error); 
      } 

     }); 

    }); 
+0

这个请求被多久发送一次?您可能希望定期发送一次(每5分钟一次?)并存储结果,然后用存储的结果进行响应,而不是在每次需要时都进行刮取。 (也有可能是一个web服务,你可以使用这个而不是刮...) –

+0

当人重新加载我们想要的温度的页面时,它会被发送。 – RayOfHope

+0

,那是每小时一次?每分钟200次? –

回答

0
console.log('before'); 
console.log($('.current-tem').find('span').html().substr(0,5)); 

你最有可能得到偶尔ECONNRESET,因为他们的网站很慢...

+0

如何知道网站是否缓慢。我的意思是选择哪个站点然后抓取当前的温度。 – RayOfHope

+0

@RayOfHope如果你在刮别人的网站,总会有一个机会,你会得到一个网络错误。 –

+0

有什么办法可以避免这个错误? – RayOfHope

0

就像我说的总是最好使用API​​像forecast.io比网页抓取更可靠。

+0

我得到这个方法有问题。但我需要在3小时内在我们的网站上取消新德里的温度并提交。你能帮我一下,让我可以改变我的API。 – RayOfHope