我正在尝试使用 nodejs 实现抓取。我正在向特定的 url 发送请求,当我收到响应时,使用响应将页面中的每个产品存储到数组中。对于每个产品,我都试图显示产品详细信息,如产品名称、价格和折扣等。我通过以下代码执行此操作。
var $products = $body.find('.fashion-item');
$products.each(function (i, item) {
var $name = ($(item).find('.info .title').text(),
$price=$(item).find('span.price.regular').text().substr(6),
$discount=$(item).find('span.price.percentoff').text().slice(0,2);
self.items[i] = {
title: $name,
price: $price,
discount: $discount,
};
});
console.log(self.items);
它工作正常。如果类名类似于“fashion-item”或“fashion-item-first”,那么所有这些都可以正常工作。但是,如果类名包含单词之间的空格,则不会将任何产品存储在数组 ($products) 中,即 $products 的数组长度为零。我的问题是,如果类名像这样的“时尚项目优先”,如何做同样的事情。我尝试了很多,但我没有任何想法。请帮我。