【发布时间】:2016-11-18 22:02:39
【问题描述】:
我正在尝试从产品结果页面中提取所有 mpn 值,其中数据以 JSON-LD 格式存储。获取前 10 个值,这是最理想的方法吗?
(function(){
var mpns= document.querySelectorAll('script[type="application/ld+json"]');
var x = [];
mpns.forEach(
function(value){
var a = JSON.parse(value.innerText).mpn;
x.push(a);
}
);
return x.slice(0,10);
})();
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "Executive Anvil",
"image": "http://www.example.com/anvil_executive.jpg",
"description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
"mpn": "925872",
"brand": {
"@type": "Thing",
"name": "ACME"
}
}
}
</script>
【问题讨论】:
标签: javascript json-ld