优化可获取Instagram帖子数据的Google脚本 [英] Optimizing google script that grabs Instagram post data

查看:108
本文介绍了优化可获取Instagram帖子数据的Google脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天/几周里,我一直在研究Google脚本,将Instagram数据导入到电子表格中.到目前为止,我已经成功地掌握了关注者,关注数据,参与率和帖子数量以及每日变化等内容.

Over the past few days/weeks I've been working on a Google Script that imports Instagram data in to a spreadsheet. So far I've successfully managed to grab things like follower, following data, engagement rate and amount of posts and daily changes.

我现在正在努力寻找一种方法,使我可以逐级获取数据并每天更新数据.但是由于我真的是编程新手.我正在以一种非常低效的方式来执行此操作,而且我不知道如何更新现有行.

I'm now working to find a way that allows me to grab the data on a post-by-post level and update the data daily. But since i'm really a novice in programming. I'm doing it in a very inefficient way and I don't have an idea how to update existing rows.

这是我到目前为止所拥有的:

This is what I have so far:

// Get Date field filled
function insertPostData(sheetName, instagramAccountName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName); 
  sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), Post1Comment(instagramAccountName)]); 
  sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), Post2Comment(instagramAccountName)]); 
  Utilities.sleep(200);
}; 



//Write post1 comments to Sheet
function Post1Comment(username) { 
  var url = "https://www.instagram.com/" + username + "/?__a=1", totalCount = 0;
  var response = UrlFetchApp.fetch(url).getContentText();
    for(var i = 0; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response).user.media.nodes[i].comments.count);
     return totalCount;
}}

//Write post2 comments to Sheet
function Post2Comment(username) { 
  var url = "https://www.instagram.com/" + username + "/?__a=1", totalCount = 0;
  var response = UrlFetchApp.fetch(url).getContentText();
    for(var i = 1; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response).user.media.nodes[i].comments.count);
     return totalCount;
}}

我需要12篇文章(您可以从 https中随意获取) ://www.instagram.com/username/?__a = 1 url.考虑一下我想从每个帖子中抓取多个对象(帖子日期,喜欢,评论,媒体ID等)的事实.这样会很低效...

And this I would need to for 12 posts (which you can freely grab from the https://www.instagram.com/username/?__a=1 url. Thinking about the fact that I want to grab multiple objects (post date, likes, comments, media id,..) from each post doing it this way would be really inefficient...

有人可以帮助找到正确的方向来提高效率吗?

Can anybody help find the right direction to do this more efficient?

推荐答案

如何使用UrlFetchApp.fetchAll()?最近,添加了此方法.此方法可以获取多个请求.

How about using UrlFetchApp.fetchAll()? Recently, this method was added. This method can fetch several requests.

fetchAll(),可以使用带有请求的数组执行请求. fetchAll()的响应是一个数组.并且响应数组中的索引与所请求数组的索引相对应.我认为这可以用于您的情况.

At fetchAll(), the requests can be executed using an array with the requests. The response from fetchAll() is an array. And the indexes in the array of response are corresponding to those of the requested array. I thought that this can be used for your situation.

在使用这种方法时,修改后的脚本如下.

When this method is used for your situation, the modified script is as follows.

  • 创建请求数组.
  • 使用创建的请求获取.
  • 转换检索到的数据以导入到电子表格.
  • 导入转换后的数据.
function insertPostData(sheetName) {
  var userNames = [
    "instagramAccountName1",
    "instagramAccountName2",
    "instagramAccountName3",
    ,
    ,
  ]; // Please input instagramAccountNames here.

  var requests = userNames.map(function(e){return {"url": "https://www.instagram.com/" + e + "/?__a=1", "method": "get"}});
  var response = UrlFetchApp.fetchAll(requests);
  var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
  var totalCounts = [];
  for (var j in response) {
    var totalCount = 0;
    for(var i = 1; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response[j]).user.media.nodes[i].comments.count);
    }
    totalCounts.push([date, totalCount]);
  }
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);
  sheet.getRange(sheet.getLastRow() + 1, 1, totalCounts.length, totalCounts[0].length).setValues(totalCounts);
}

参考:

  • fetchAll()
  • Reference :

    • fetchAll()
    • 如果我误解了你的问题,对不起.如果这不起作用,请告诉我.我想修改.

      If I misunderstand your question, I'm sorry. If this didn't work, please tell me. I would like to modify.

      这篇关于优化可获取Instagram帖子数据的Google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆