javascript - cheerio 脚本元素 - 数据

标签 javascript cheerio

我正在使用 cheerio 进行一些抓取,并希望访问页面上的 head js 元素(尤其是 instructables)。我可以访问它,但它作为功能返回。

所以使用这个:

  console.log($('script').attr('type', "application/ld+json").text);

最佳答案

这是因为$("script").attr("type","application/ld+json");返回了一组script标签(上面不止一个script标签页面),它还将页面的所有脚本标记的类型更改为 application/ld+json

See JQuery .attr() documentation.

如果你需要得到那种类型的$("script[type='application/ld+json']") 就可以了。

var request = require('request');
var cheerio = require('cheerio');

request('http://www.instructables.com/id/Making-an-online-Fish-Tank-webcam!/step3/Cut-the-project-box/', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    var obj = $("script[type='application/ld+json']"); 

    for(var i in obj){
        for(var j in obj[i].children){
            var data = obj[i].children[j].data;
            if(data){
               console.log(data);
            }
        }
    }
  }
});

关于javascript - cheerio 脚本元素 - 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43053213/

相关文章:

javascript - 网格容器中的无限行

javascript - 主体加载后未调用函数

javascript - v-autocomplete on-select on-remove in vuetify

node.js - 无法使用链接获取不同的标题

javascript - 如何获得两个日期之间的差异?

javascript - JSTREE 在第二次加载时失败

node.js - 使用请求时响应中的正文为空

javascript - 在 cheerio 中创建新元素

javascript - 如何用cheerio获取数据?当我在页面源数据中看到为空时,但是当我在检查元素中看到时,我看到了数据

node.js - 有人可以帮我传递 URL 作为参数吗