我一直试图取消cheerio的以下网页,以便为我的一个小项目获得最新的温湿度速率:网站链接
不幸的是,我似乎要挖很多标签,我找不到我的路。我试着检查这个元素以查看它的css路径,但是没有用。我尝试过的代码如下(它基于我检查元素时得到的css选择器):
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);我只是得到了‘空’,所以它似乎不起作用。
如果有人能在这件事上帮我的忙,那将是一个巨大的帮助!
提前感谢
发布于 2014-07-02 21:59:39
所以我找到了解决问题的方法。但它很丑。我想找一种更优雅的方法来做这件事!
$ = 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];欢迎任何帮助、建议或意见!
https://stackoverflow.com/questions/24530540
复制相似问题