我正在尝试获取带字幕的instagram个人资料的帖子,除字幕外,其他一切正常。它显示:[object Object]
我还没有使用过任何api。
我的代码:
function nFormatter(num){
if(num >= 1000000000){
return (num/1000000000).toFixed(1).replace(/\.0$/,'') + 'G';
}
if(num >= 1000000){
return (num/1000000).toFixed(1).replace(/\.0$/,'') + 'M';
}
if(num >= 1000){
return (num/1000).toFixed(1).replace(/\.0$/,'') + 'K';
}
return num;
}
$.ajax({
url:"https://www.instagram.com/bhudiptaakash?__a=1",
type:'get',
success:function(response){
$(".profile-pic").attr('src',response.graphql.user.profile_pic_url);
posts = response.graphql.user.edge_owner_to_timeline_media.edges;
posts_html = '';
for(var i=0;i<posts.length;i++){
caption = posts[i].node.edge_media_to_caption;
likes = posts[i].node.edge_liked_by.count;
posts_html += '<a href="https://instagram.com/p/'+shortcode+'">: '+caption+';
}
$(".posts").html(posts_html);
}
});
我该如何解决?
edge_media_to_caption
属性的值是这样的对象:
{
"edges": [{
"node": {
"text": "Still alive"
}
}]
}
您需要遍历边缘并获得node.text
属性。
for (var i = 0; i < posts.length; i++) {
let caption = posts[i].node.edge_media_to_caption.edges.map(e => e.node.text).join('<br>');
let likes = posts[i].node.edge_liked_by.count;
posts_html += '<a href="https://instagram.com/p/' + shortcode + '">: ' + caption + '</a>';
}