javascript - 如何使用CheerioJS获取shopmissa.com的文章

标签 javascript node.js axios cheerio

我正在尝试获取这篇文章的所有文章。 This is my webiste so far .

但是我不擅长使用Cheerio,我只知道如何获取简单的元素。
并且,例如,产品的 URL 图像有很多子元素。

我需要的数据是:

  • 文章名称,
  • 价格,
  • 网址,
  • 图片。

我开始尝试获取所有文章图像:

axios.get("https://www.shopmissa.com/collections/eye-shadow")
     .then(res => 
       { if(res.status == 200) 
         { const html = res.data; 
           const $ = cheerio.load(html); 
           $(".product-index").each((i, elem) => 
             { console.log($(this)
               .children(".prod-container")
               .children(".prod-image") 
               .find("a")
               .children("reveal")
               .find("img")
               .attr("src")
             ) 
           }
         ) 
       } 
     }, error => console.log(error)
   )

但我不了解全部情况...
我能做什么?

最佳答案

不要试图专注于遍历每个 DOM,而是使用选择器让 Cheerio 完成繁重的工作。

axios.get("https://www.shopmissa.com/collections/eye-shadow")
  .then(res => {
    if (res.status == 200) {
      const html = res.data;
      const $ = cheerio.load(html);
      $("#product-loop").children().each((i, elem) => {
        var imageSource = $(elem).find(".reveal img").attr("src")
        var productName = $(elem).find(".product-info h3").text()
        var productPrice = $(elem).find(".product-info .money").text()
        console.log(imageSource, productName, productPrice)
      })
    }
  }, error => console.log(error))

关于javascript - 如何使用CheerioJS获取shopmissa.com的文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53213942/

相关文章:

node.js - 我可以从 jwt 键中删除除第一个和最后一个以外的所有换行符吗

javascript - redux-saga catch 加载资源失败:net::ERR_CONNECTION_REFUSED

azure - Microsoft 从 oauth2 登录问题

javascript - 数组中元素的奇数个

javascript - e.preventDefault() 与 PreventDefault(e)

javascript - NodeJS 发布请求 - 参数中的数字返回 NaN

vue.js - 在 vuex 操作中具有多个参数的 Axios post 请求

javascript - 如何循环遍历对象数组,将键推送到另一个数组

javascript - 循环调用api直到响应成功

node.js - 从 Node 获取mp3文件以作出 react