【问题标题】:Cant get image src with cheerio无法使用 Cheerio 获取图像 src
【发布时间】:2021-06-08 20:05:46
【问题描述】:

我正在尝试将 scraper 与 nodejs 和cheerio 结合起来。 到目前为止我有这个:

class ScraperService {

    static getDwelling(url) {
        const dwelling = {
            images: []
        };
        return new Promise((resolve, reject) => {
            request(`https://www.zonaprop.com.ar/propiedades/${url}`, (err, resp, html) => {
                if(err || resp.statusCode === 404) {
                    return reject(err);
                }
                const $ = cheerio.load(html);
                pe = $('.price-operation', '#article-container').text();
                dwelling.price = $('.price-items', '#article-container').text();
                dwelling.description = $('.description-container', '#article-container').html();
                //getting images here
                $('#tab-foto-flickity').find('img').each(() => {dwelling.images.push(this);});
                resolve(dwelling);
            });
        });
    }
}

module.exports = ScraperService;

问题是我在图片的链接中插入了 37 个空值,我尝试了不同的标签,但没有运气。任何想法代码在哪里失败

【问题讨论】:

    标签: node.js cheerio


    【解决方案1】:

    您得到空值的原因是由于在each 回调中使用了箭头函数() =>{}。 箭头函数没有this。尝试使用经典的匿名函数。

    见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 2016-03-17
      • 2023-03-03
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多