简体   繁体   English

使用Cheerio和Response for Node Web抓取工具,将响应函数结果传递给视图

[英]Using Cheerio and Response for Node web scraper, passing response function result to the view

I'm using this tutorial : https://www.smashingmagazine.com/2015/04/web-scraping-with-nodejs/ 我正在使用本教程: https : //www.smashingmagazine.com/2015/04/web-scraping-with-nodejs/

To make a really basic node web scraper. 制作一个真正的基本节点网络刮板。 I have my app set up and running here: 我在这里设置并运行我的应用程序:

https://[redacted]/ https:// [已编辑] /

What it is currently doing behind the scenes in my node app, is using the two modules cheerio and request to run this function (below). 目前,我的节点应用程序在后台执行的操作是使用两个模块cheerio并请求运行此功能(如下所示)。 This function basically takes a URL, makes the request and grabs an element of the page with a data variable, scrapes the value of it, and logs the value, (temperature) to the console of my terminal of my computer. 此函数基本上获取一个URL,发出请求,并使用数据变量获取页面的元素,然后抓取其值,并将该值(温度)记录到计算机终端的控制台上。 I need a way to send this value to my view and render it on the page instead of just logging it to the console of my computer. 我需要一种方法将此值发送到我的视图并在页面上呈现它,而不仅仅是将其记录到计算机的控制台中。

The problem I'm having is that the scope of the request function below, I can't pass any of the return values, (temperature) to my view. 我遇到的问题是以下请求函数的范围,我无法将任何返回值(温度)传递给我的视图。 I know there is something wrong with my set up because I currently have the request function INSIDE of my router.get. 我知道我的设置有问题,因为我当前拥有router.get的请求功能INSIDE。 If I put the request function outside the router.get, I still cant pass the values to my view but it will successfully get the data from the web url and log it to my terminal's console. 如果我将请求功能放在router.get之外,我仍然无法将值传递到我的视图,但是它将成功从Web URL获取数据并将其记录到终端的控制台。 I hope I am being clear. 我希望我很清楚。 Please see the res.render to my view which wraps the request function that is doing the web scraping.. 请查看res.render到我的视图,该视图包装了执行Web抓取的request函数。

router.get('/', function(req, res, next) {

    //target url we are scraping data from
    var url = "http://www.wunderground.com/cgi-bin/findweather/getForecast?&query=" + 02888;
    var temperature;
    // request function that uses the request module
    request(url, function (error, response, body) {

        if (!error) {
            // using cheerio module to load body of html req document and scrape the data
            var $ = cheerio.load(body),
                temperature = $("[data-variable='temperature'] .wx-value").html();
            // logs it to console (of my computer..)    
            console.log("It’s " + temperature + " degrees Fahrenheit.");
        } 

        else {
            console.log("We’ve encountered an error: " + error);
        }

        return temperature;
    });
    /* renders my view, this is my attempt to pass the values as variables to my handlebars template. Currently it is only passing the URL var as it exists before the request function call, and the empty var temperature (which gets its value during, and inside the request function call). How can i get at those values returned from the request function and pass to my view below? */
    res.render('index', {title: url, data: temperature } );  

});

The function in request is a executed asynchronously, so as you have it, render is getting called before temperature is set. request的函数是异步执行的,因此,正如您所拥有的, render是在设置温度之前被调用的。 You need to move the render function into the async function. 您需要将render功能移入异步功能。

router.get('/', function(req, res, next) {

//target url we are scraping data from
var url = "http://www.wunderground.com/cgi-bin/findweather/getForecast?&query=" + 02888;
var temperature;
// request function that uses the request module
request(url, function (error, response, body) {

    if (!error) {
        // using cheerio module to load body of html req document and scrape the data
        var $ = cheerio.load(body),
            temperature = $("[data-variable='temperature'] .wx-value").html();
        // logs it to console (of my computer..)    
        console.log("It’s " + temperature + " degrees Fahrenheit.");
    } 

    else {
        console.log("We’ve encountered an error: " + error);
    }

    res.render('index', {title: url, data: temperature } );  
});

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM