【问题标题】:Not able to crawl the correct data无法抓取正确的数据
【发布时间】:2016-03-04 11:26:45
【问题描述】:

我想从以下网址抓取德里的当前温度。我正在使用 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);
            }

        });

    });

【问题讨论】:

  • 这个请求多久发送一次?您可能希望定期发送它(每 5 分钟一次?)并存储结果,然后用存储的结果进行响应,而不是每次需要时都抓取。 (您可能还可以使用 Web 服务来代替抓取...)
  • 当这个人重新加载我们想要临时的页面时发送它。
  • 而且,是每小时一次吗?每分钟 200 次?
  • 定期请求它可以让您通过简单地返回最后一个成功的值来缓解网络错误,直到另一个请求成功更新该值。
  • 为什么不使用类似的 api:developer.forecast.io

标签: jquery node.js web-scraping


【解决方案1】:
console.log('before');
console.log($('.current-tem').find('span').html().substr(0,5));

您很可能偶尔会收到 ECONNRESET,因为他们的网站速度很慢...

【讨论】:

  • 如何知道网站是否慢。我的意思是选择哪个站点来抓取当前的温度。
  • @RayOfHope 如果你正在抓取别人的网站,你总是有可能遇到网络错误。
  • 将请求超时选项设置为较大的值(例如 600 秒)。这可能会有所帮助...
  • 怎么做?我必须在这个api中设置吗?
  • request({url:url,timeout:600},function(error,response,html))
【解决方案2】:

正如我所说,总是更好地使用像 forecast.io 这样的 api,而不是网络抓取。

【讨论】:

  • 我知道这个方法有问题。但我需要在 3 小时内在我们的网站上报废新德里的 temp 并提交。你能帮我一个办法,以便我可以在我的 api 中进行更改。
猜你喜欢
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2022-11-29
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多