我正在尝试在使用cheerio抓取Web的节点服务器中执行一个功能,问题是由于某些原因,我的功能无法正常运行,
控制器:
class ScrapperController {
static async scrapDwelling(req, res, next) {
try {
const dwelling = await ScrapperService.getDwelling(req.params.url);
console.log(dwelling);
res.send({dwelling});
} catch (err) {
next(err);
}
}
}
然后提供我的服务
static async getDwelling(url) {
const dwelling = {};
await request(`https://www.zonaprop.com.ar/propiedades/${url}`, (err, resp, html) => {
const $ = cheerio.load(html);
dwelling.type = $('.price-operation', '#article-container').text();
dwelling.price = $('.price-items', '#article-container').text();
dwelling.description = $('.section-description', '#article-container').text();
dwelling.title = $('.title-type-sup > b').text();
dwelling.location = $('.title-location > b').text();
const coordinatesHelper = ($('.static-map', '#article-map').attr('src'));
const coordinates = coordinatesHelper.substring(
coordinatesHelper.lastIndexOf('markers=') + 8,
coordinatesHelper.lastIndexOf('&channel')
);
dwelling.coordinates = coordinates;
console.log($('#tab-foto-flickity').find('img').length);
return dwelling;
});
return dwelling;
}
由于某些原因,您看到控制台日志时,该函数先返回然后执行代码。我在控制台中得到这个:
{}
GET /public-api/scrapper/42998731.html 200 6.559毫秒-15
36
答案 0 :(得分:4)
request
节点模块不返回承诺,它使用回调函数。您可以将请求手动包装在Promise中,如下所示:
static getDwelling(url) {
return new Promise((resolve, reject) => {
request(`https://www.zonaprop.com.ar/propiedades/${url}`, (err, resp, html) => {
if(err) {
return reject(err);
}
resolve(html);
});
}).then((html) => {
const $ = cheerio.load(html);
const dwelling = {};
dwelling.type = $('.price-operation', '#article-container').text();
dwelling.price = $('.price-items', '#article-container').text();
dwelling.description = $('.section-description', '#article-container').text();
dwelling.title = $('.title-type-sup > b').text();
dwelling.location = $('.title-location > b').text();
const coordinatesHelper = ($('.static-map', '#article-map').attr('src'));
const coordinates = coordinatesHelper.substring(
coordinatesHelper.lastIndexOf('markers=') + 8,
coordinatesHelper.lastIndexOf('&channel')
);
dwelling.coordinates = coordinates;
console.log($('#tab-foto-flickity').find('img').length);
return dwelling;
});
}
或者您可以使用request-promise-native之类的库。