Instagram JSON TypeError: Cannot read property "text" from undefined
Instagram JSON TypeError: Cannot read property "text" from undefined
我正在尝试使用 Google Sheet 为 Instagram 构建分析,但是当我想要 return 图片中的标题文本 url.
我已经尝试了我的代码的不同变体,但它总是同一个问题。我的结果要么返回(在输出中)为 "undefined",要么只是抛出错误。
function MultipleHashtags() {
//Set up multiple sheets and instagram accounts
var hashtagArray = ["fitness", "fashion"];
for(var i = 0; i < hashtagArray.length; i++) { grabData( hashtagArray[i]); };
//Grab data from TOP 9 posts
function grabData(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
for(var i = 0; i < 9; i++) {
var hashtag = JSON.parse(response).graphql.hashtag.name;
var postOwner = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.owner.id;
var shortCode = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.shortcode;
var URL = "https://www.instagram.com/p/"+shortCode;
var isVideo = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.is_video;
var PostComments = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_comment.count;
var PostLikes = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_liked_by.count;
var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), hashtag, postOwner, shortCode, URL, isVideo, PostComments, PostLikes, caption])
var range = sheet.getRange("A2:H")
range.sort([{column: 1, ascending: false}]);
Utilities.sleep(200);;
}
}
};
可能还不是最好的代码,但我仍在学习 :)。任何帮助,将不胜感激。
这个脚本所做的是读取这个 URL 后面的转储:
https://www.instagram.com/explore/tags/fashion/?__a=1
例如:
"graphql":{"hashtag":{"name":"fashion","is_top_media_only":false,"profile_pic_url":"https://scontent-bru2-1.cdninstagram.com/vp/3919f4813f1759cd589c9e256fafa05b/5B584579/t51.2885-15/s150x150/e35/c0.135.1080.1080/29717343_617997658538341_2606017788199829504_n.jpg","edge_hashtag_to_media":{"count":494984995,"page_info":{"has_next_page":true,"end_cursor":"AQBYZqz6Bc4vBlwhzRuhZm1NQSk7HQmHsJ9ZdMzanB-4UAH9rfDCjuWq-iqx5iEEaT0pPeJ_2aydWn_ZToa_aR5wt2ww743GTMbvIfx1lEAlpQ"},"edges":[{"node":{"comments_disabled":false,"id":"1750034539685082052","edge_media_to_caption":{"edges":[{"node":{"text":"NEW | Our new webshop is online!\n\nwww.ivan-online.nl\nFrom now on you can order your shawl easily online. A special gift for yourself, or for someone else. A warm shawl with a warm story.\n\nLet us know what you think about it \ud83d\ude4f \u2014\n\u2014\n#webshop #fashion #shawl #sjaal #cashmere #kasjmir #internetfashion #fashionable #handmade #nepal #fairtrade #springfashion #2018\n#photo"}}]},"shortcode":"BhJXyRKl_PE","edge_media_to_comment":{"count":0},"taken_at_timestamp":1522840395,"dimensions":{"height":720,"width":1080},"display_url":"https://scontent-bru2-1.cdninstagram.com/vp/cbf53cb645805430051bc7b056fb1dbf/5B643AD8/t51.2885-15/e35/29414609_430637507388196_1941728523811028992_n.jpg","edge_liked_by":{"count":0},"edge_media_preview_like":{"count":0},"owner":{"id":"5998662494"},"thumbnail_src":"https://scontent-bru2-1.cdninstagram.com/vp/68d5e9377dee9bfb0165bfa18473b6c6/5B56C8D8/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","thumbnail_resources":[{"src":"https://scontent-bru2-1.cdninstagram.com/vp/e8a4b97569e409822c382ddb3231af00/5B62B69B/t51.2885-15/s150x150/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":150,"config_height":150},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/fbc573c382379e29a4b321b1b5e423c3/5B5A190D/t51.2885-15/s240x240/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":240,"config_height":240},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/a5b50f4711a8ab1774320c912cb70b7c/5B620CE5/t51.2885-15/s320x320/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":320,"config_height":320},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/b27613e12796dfb97803acd4e429c20d/5B5C9FCC/t51.2885-15/s480x480/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":480,"config_height":480},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/68d5e9377dee9bfb0165bfa18473b6c6/5B56C8D8/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":640,"config_height":640}],"is_video":false}}
其中有以下一行很有趣:
"edge_media_to_caption":{"edges":[{"node":{"text":"NEW | Our new webshop is online!\n\nwww.ivan-online.nl\nFrom now on you can order your shawl easily online. A special gift for yourself, or for someone else. A warm shawl with a warm story.\n\nLet us know what you think about it \ud83d\ude4f \u2014\n\u2014\n#webshop #fashion #shawl #sjaal #cashmere #kasjmir #internetfashion #fashionable #handmade #nepal #fairtrade #springfashion #2018\n#photo"}}]}
我想抓住的是:
var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
而这个 return 是错误:
TypeError: Cannot read property "text" from undefined
来自堆栈驱动程序:
2018-04-04 14:20:42.316 CEST
TypeError: Cannot read property "text" from undefined. at grabData(GrabData:21) at MultipleHashtags(GrabData:5)
Expand all | Collapse all {
insertId: "198nrphg1tmmbn6"
jsonPayload: {…}
labels: {…}
logName: "projects/project-id-1872200188033847311/logs/script.googleapis.com%2Fconsole_logs"
receiveTimestamp: "2018-04-04T12:20:43.328274499Z"
resource: {…}
severity: "ERROR"
timestamp: "2018-04-04T12:20:42.316Z"
}
您收到错误的原因是您试图访问不存在的 object 属性。不幸的是,这是大量嵌套的数据,因此您需要在此处进行大量检查。有多种方法可以做到这一点,但我使用 this 作为指导。
在下面的代码中,我分解了您的 grabData()
函数,因为我个人不喜欢嵌套函数。
function MultipleHashtags() {
//Set up multiple sheets and instagram accounts
var hashtagArray = ["fitness", "fashion"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var range = sheet.getRange("A2:H")
range.sort([{column: 1, ascending: false}]);
Utilities.sleep(200);;
for(var i = 0; i < hashtagArray.length; i++) {
var data = grabData(hashtagArray[i]);
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), data.hashtag, data.postOwner, data.shortCode, data.URL, data.isVideo, data.PostComments, data.PostLikes, data.caption]);
}
}
//Grab data from TOP 9 posts
function grabData(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
var responseObject = JSON.parse(response);
var hashtagData = responseObject.graphql.hashtag;
var data = {};
for(var i = 0; i < 9; i++) {
var hashtag = hashtagData.name;
if (checkNested(hashtagData, "edge_hashtag_to_top_posts", "edges")) {
var edge = hashtagData.edge_hashtag_to_top_posts.edges[i];
if (checkNested(edge, "node")) {
var node = edge.node;
var postOwner = checkNested(node, "owner", "id") ? node.owner.id : "";
var shortCode = checkNested(node, "shortcode") ? node.shortcode : "";
var URL = "https://www.instagram.com/p/"+shortCode;
var isVideo = checkNested(node, "is_video") ? node.is_video : "";
var PostComments = checkNested(node, "edge_media_to_comment", "count") ? node.edge_media_to_comment.count : "";
var PostLikes = checkNested(node, "edge_liked_by", "count") ? node.edge_liked_by.count : "";
var caption = "";
if (checkNested(node, "edge_media_to_caption", "edges")) {
var captions = node.edge_media_to_caption.edges[0];
caption = checkNested(captions, "node", "text") ? captions.node.text : "";
}
}
}
data = {
"hashtag": hashtag,
"postOwner": postOwner,
"shortCode": shortCode,
"URL": URL,
"isVideo": isVideo,
"PostComments": PostComments,
"PostLikes": PostLikes,
"caption": caption
};
}
return data;
}
/**
* Test for existence of nested JavaScript object key
* Copied from https://whosebug.com/questions/2631001/test-for-existence-of-nested-javascript-object-key
*/
function checkNested(obj /*, level1, level2, ... levelN*/) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
}
编辑:忘记了有关原始代码的要点。当您访问标题 var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
时,最后一个 edges
实际上是一个数组。在我的代码中,我只选择了该数组的第一个元素。您可以根据需要进行更新。
我正在尝试使用 Google Sheet 为 Instagram 构建分析,但是当我想要 return 图片中的标题文本 url.
我已经尝试了我的代码的不同变体,但它总是同一个问题。我的结果要么返回(在输出中)为 "undefined",要么只是抛出错误。
function MultipleHashtags() {
//Set up multiple sheets and instagram accounts
var hashtagArray = ["fitness", "fashion"];
for(var i = 0; i < hashtagArray.length; i++) { grabData( hashtagArray[i]); };
//Grab data from TOP 9 posts
function grabData(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
for(var i = 0; i < 9; i++) {
var hashtag = JSON.parse(response).graphql.hashtag.name;
var postOwner = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.owner.id;
var shortCode = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.shortcode;
var URL = "https://www.instagram.com/p/"+shortCode;
var isVideo = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.is_video;
var PostComments = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_comment.count;
var PostLikes = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_liked_by.count;
var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), hashtag, postOwner, shortCode, URL, isVideo, PostComments, PostLikes, caption])
var range = sheet.getRange("A2:H")
range.sort([{column: 1, ascending: false}]);
Utilities.sleep(200);;
}
}
};
可能还不是最好的代码,但我仍在学习 :)。任何帮助,将不胜感激。
这个脚本所做的是读取这个 URL 后面的转储: https://www.instagram.com/explore/tags/fashion/?__a=1
例如:
"graphql":{"hashtag":{"name":"fashion","is_top_media_only":false,"profile_pic_url":"https://scontent-bru2-1.cdninstagram.com/vp/3919f4813f1759cd589c9e256fafa05b/5B584579/t51.2885-15/s150x150/e35/c0.135.1080.1080/29717343_617997658538341_2606017788199829504_n.jpg","edge_hashtag_to_media":{"count":494984995,"page_info":{"has_next_page":true,"end_cursor":"AQBYZqz6Bc4vBlwhzRuhZm1NQSk7HQmHsJ9ZdMzanB-4UAH9rfDCjuWq-iqx5iEEaT0pPeJ_2aydWn_ZToa_aR5wt2ww743GTMbvIfx1lEAlpQ"},"edges":[{"node":{"comments_disabled":false,"id":"1750034539685082052","edge_media_to_caption":{"edges":[{"node":{"text":"NEW | Our new webshop is online!\n\nwww.ivan-online.nl\nFrom now on you can order your shawl easily online. A special gift for yourself, or for someone else. A warm shawl with a warm story.\n\nLet us know what you think about it \ud83d\ude4f \u2014\n\u2014\n#webshop #fashion #shawl #sjaal #cashmere #kasjmir #internetfashion #fashionable #handmade #nepal #fairtrade #springfashion #2018\n#photo"}}]},"shortcode":"BhJXyRKl_PE","edge_media_to_comment":{"count":0},"taken_at_timestamp":1522840395,"dimensions":{"height":720,"width":1080},"display_url":"https://scontent-bru2-1.cdninstagram.com/vp/cbf53cb645805430051bc7b056fb1dbf/5B643AD8/t51.2885-15/e35/29414609_430637507388196_1941728523811028992_n.jpg","edge_liked_by":{"count":0},"edge_media_preview_like":{"count":0},"owner":{"id":"5998662494"},"thumbnail_src":"https://scontent-bru2-1.cdninstagram.com/vp/68d5e9377dee9bfb0165bfa18473b6c6/5B56C8D8/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","thumbnail_resources":[{"src":"https://scontent-bru2-1.cdninstagram.com/vp/e8a4b97569e409822c382ddb3231af00/5B62B69B/t51.2885-15/s150x150/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":150,"config_height":150},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/fbc573c382379e29a4b321b1b5e423c3/5B5A190D/t51.2885-15/s240x240/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":240,"config_height":240},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/a5b50f4711a8ab1774320c912cb70b7c/5B620CE5/t51.2885-15/s320x320/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":320,"config_height":320},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/b27613e12796dfb97803acd4e429c20d/5B5C9FCC/t51.2885-15/s480x480/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":480,"config_height":480},{"src":"https://scontent-bru2-1.cdninstagram.com/vp/68d5e9377dee9bfb0165bfa18473b6c6/5B56C8D8/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/29414609_430637507388196_1941728523811028992_n.jpg","config_width":640,"config_height":640}],"is_video":false}}
其中有以下一行很有趣:
"edge_media_to_caption":{"edges":[{"node":{"text":"NEW | Our new webshop is online!\n\nwww.ivan-online.nl\nFrom now on you can order your shawl easily online. A special gift for yourself, or for someone else. A warm shawl with a warm story.\n\nLet us know what you think about it \ud83d\ude4f \u2014\n\u2014\n#webshop #fashion #shawl #sjaal #cashmere #kasjmir #internetfashion #fashionable #handmade #nepal #fairtrade #springfashion #2018\n#photo"}}]}
我想抓住的是:
var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
而这个 return 是错误:
TypeError: Cannot read property "text" from undefined
来自堆栈驱动程序:
2018-04-04 14:20:42.316 CEST TypeError: Cannot read property "text" from undefined. at grabData(GrabData:21) at MultipleHashtags(GrabData:5) Expand all | Collapse all { insertId: "198nrphg1tmmbn6"
jsonPayload: {…}
labels: {…}
logName: "projects/project-id-1872200188033847311/logs/script.googleapis.com%2Fconsole_logs"
receiveTimestamp: "2018-04-04T12:20:43.328274499Z"
resource: {…}
severity: "ERROR"
timestamp: "2018-04-04T12:20:42.316Z"
}
您收到错误的原因是您试图访问不存在的 object 属性。不幸的是,这是大量嵌套的数据,因此您需要在此处进行大量检查。有多种方法可以做到这一点,但我使用 this 作为指导。
在下面的代码中,我分解了您的 grabData()
函数,因为我个人不喜欢嵌套函数。
function MultipleHashtags() {
//Set up multiple sheets and instagram accounts
var hashtagArray = ["fitness", "fashion"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var range = sheet.getRange("A2:H")
range.sort([{column: 1, ascending: false}]);
Utilities.sleep(200);;
for(var i = 0; i < hashtagArray.length; i++) {
var data = grabData(hashtagArray[i]);
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), data.hashtag, data.postOwner, data.shortCode, data.URL, data.isVideo, data.PostComments, data.PostLikes, data.caption]);
}
}
//Grab data from TOP 9 posts
function grabData(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
var responseObject = JSON.parse(response);
var hashtagData = responseObject.graphql.hashtag;
var data = {};
for(var i = 0; i < 9; i++) {
var hashtag = hashtagData.name;
if (checkNested(hashtagData, "edge_hashtag_to_top_posts", "edges")) {
var edge = hashtagData.edge_hashtag_to_top_posts.edges[i];
if (checkNested(edge, "node")) {
var node = edge.node;
var postOwner = checkNested(node, "owner", "id") ? node.owner.id : "";
var shortCode = checkNested(node, "shortcode") ? node.shortcode : "";
var URL = "https://www.instagram.com/p/"+shortCode;
var isVideo = checkNested(node, "is_video") ? node.is_video : "";
var PostComments = checkNested(node, "edge_media_to_comment", "count") ? node.edge_media_to_comment.count : "";
var PostLikes = checkNested(node, "edge_liked_by", "count") ? node.edge_liked_by.count : "";
var caption = "";
if (checkNested(node, "edge_media_to_caption", "edges")) {
var captions = node.edge_media_to_caption.edges[0];
caption = checkNested(captions, "node", "text") ? captions.node.text : "";
}
}
}
data = {
"hashtag": hashtag,
"postOwner": postOwner,
"shortCode": shortCode,
"URL": URL,
"isVideo": isVideo,
"PostComments": PostComments,
"PostLikes": PostLikes,
"caption": caption
};
}
return data;
}
/**
* Test for existence of nested JavaScript object key
* Copied from https://whosebug.com/questions/2631001/test-for-existence-of-nested-javascript-object-key
*/
function checkNested(obj /*, level1, level2, ... levelN*/) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
}
编辑:忘记了有关原始代码的要点。当您访问标题 var caption = JSON.parse(response).graphql.hashtag.edge_hashtag_to_top_posts.edges[i].node.edge_media_to_caption.edges.node.text;
时,最后一个 edges
实际上是一个数组。在我的代码中,我只选择了该数组的第一个元素。您可以根据需要进行更新。