admin管理员组文章数量:1389754
Im using cheerio to do some scraping and want to access the head js element on a page (notably instructables). I can access it but it es back as function.
So using this:
console.log($('script').attr('type', "application/ld+json").text);
- (on this: !/step3/Cut-the-project-box/)
this provides 'Function'
I thought strinify would work but it doesn't :(
Im using cheerio to do some scraping and want to access the head js element on a page (notably instructables). I can access it but it es back as function.
So using this:
console.log($('script').attr('type', "application/ld+json").text);
- (on this: http://www.instructables./id/Making-an-online-Fish-Tank-webcam!/step3/Cut-the-project-box/)
this provides 'Function'
I thought strinify would work but it doesn't :(
- 1 text() is a function to get the text content of the tag, which is probably empty if the script has a .src. what are you trying to get? – dandavis Commented Mar 27, 2017 at 17:54
- It's not a src. It's the content of a script tag. .text and .data seem to be giving the same thing. runkit./58d97359d2dfbd0014d162e2/58d97359d2dfbd0014d162e3 – willwade Commented Mar 27, 2017 at 21:47
1 Answer
Reset to default 8This is because $("script").attr("type","application/ld+json");
returns an array of script tags (there's more than only one script tag on the page) and it's also changing the type of all script tag of the page to application/ld+json
See JQuery .attr() documentation.
If you need to get the one with that type $("script[type='application/ld+json']")
will do the trick.
var request = require('request');
var cheerio = require('cheerio');
request('http://www.instructables./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);
}
}
}
}
});
本文标签: javascriptcheerio script elementdataStack Overflow
版权声明:本文标题:javascript - cheerio script element - data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744734000a2622215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论