我正试图从以下餐厅搜集一些菜单信息:红牛网址:" http://redcowmn.com/50th-street-menu/" 我想要菜单项目名称和价格,现在我很麻烦只是获取名称信息。有人可以帮我解决我的代码问题吗?谢谢。 我尝试了以下代码:
var request = require("request");
var cheerio = require("cheerio");
var fs = require("fs");
var url = "http://redcowmn.com/50th-street-menu/";
request(url, function(err, response, html) {
if(!err) {
var $ = cheerio.load(html);
var allItems = $("#block-yui_3_17_2_9_1423750151911_47600").children();
var items = [];
allItems.each(function(index) {
var result = $("#block-yui_3_17_2_9_1423750151911_47600").children().eq(0).children().eq(5).find("#menu-item-title").text();
if(result !== "") {
items.push(result);
}
});
fs.writeFile("output1.txt", JSON.stringify(items, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("Data has been added to a file.");
}
})
console.log(items);
}
});