0

I've been trying to scrap the following webpage with cheerio in order to get the latest temperature and humidity rate for a little project of mine: Website link

Unfortunately, it seems like I've got to dig through a lot of tags and I can't find my way around. I've tried to inspect the element to see its css path but it did no good. The code I've tried is the following (it's based on the css selector I get when I inspect the element):

setInterval(function getTempAndHumidity() 
{
    var url = 
    {
        url: "http://www.meteociel.fr/temps-reel/obs_villes.php?code2=7630",
        method: 'GET',
        proxy: webproxy
    };

    request(url, function (error, response, body) 
    {
        if (!error && response.statusCode == 200) 
        {
            $ = cheerio.load(body);           
            console.log($('tr.texte > td:nth-child(2) > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > center:nth-child(18) > table:nth-child(3) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(5) > div:nth-child(1)').html());
        }
        else
        {
            console.log("Error when getting the temperature and humidity rate: " + error);
        }
    })
}, 2000);

I just get 'null' so it doesn't seem to work.

It would be a huge help if someone could give me a hand on this matter!

Thanks in advance

4

1 に答える 1

0

だから私は私の問題を回避する方法を見つけました。しかし、それは非常に醜いです。これを行うためのよりエレガントな方法を見つけたいです!

$ = cheerio.load(body);
var content = $('h1').parent().nextAll().nextAll().next().text();
var catch_values = content.match(/.*km\s+(\d+\.\d+).*(\d\d)%.*/);
var temp = catch_values[1];
var humid_rate = catch_values[2];

ヘルプ、アドバイス、または入力は大歓迎です!

于 2014-07-02T21:59:39.630 に答える