如何使用 Nodejs 从该 URL 获取 Instagram JSON 数据

问题描述

我和我的朋友正在尝试从 Instagram 帖子中获取评论。我们离成功还有很长的路要走,但最近 3 天我们遇到了这样的问题:

我们有一个 JSON URL:https://www.instagram.com/graphql/query/?query_hash=bc3296d1ce80a24b1b6e40b1e72903f5&variables=%7B%22shortcode%22%3A%22CKW-_36HT9R%22%2C%22first%22%3A12%2C%22after%22%3A%22%7B%5C%22cached_comments_cursor%5C%22%3A+%5C%2217903955625713230%5C%22%2C+%5C%22bifilter_token%5C%22%3A+%5C%22KFIBDgCYADgAKAAYABAACAAIAL9u8Sdv-_Xed8_jv77_6fu27_5uNa_FZrhD-dowrn_sH_n6nf3__vf_vvfL___rv__T___uvv5pKDP0ehdBf6AgAA%3D%3D%5C%22%7D%22%7D,我们正在尝试访问这些数据。

当我们触发我在下面分享代码时,我们遇到了一个交叉错误。我也分享了这个错误

此外,当我们尝试在没有登录的情况下获取数据时,我们遇到了“您应该登录”之类的错误。所以我们解决了那部分并成功登录。这是我们的代码

1- 打开 Instagram 2-登录后到达发布 3- 单击“加号 (+)”按钮以获取更多数据和 JSON URL 4- 尝试从 JSON URL 获取该数据但失败。

const puppeteer = require('puppeteer')
const results = [];
const list = [];
const list2 = [];
const fetch = require('node-fetch');
const { setTimeout } = require('timers');
const { error } = require('console');

(async () => {
    const browser = await puppeteer.launch({
        headless: false
    });
    const page = await browser.newPage();
    await page.goto('https://www.instagram.com/accounts/login/');
    await page.waitForSelector('input[name="username"]');
    await page.type('input[name="username"]','acc-id');
    await page.type('input[name="password"]','acc-pass');
    await page.click('button[type="submit"]');
    await page.waitForSelector('#react-root > section > main > div > div > div > section > div > button');
    await page.click('#react-root > section > main > div > div > div > section > div > button');
    await page.waitForSelector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm');
    await page.click('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm');
    await page.goto("https://www.instagram.com/p/CKW-_36HT9R/",{
        waitUntil: 'networkidle2'
    });
        setTimeout(() => {
            page.click('#react-root > section > main > div > div.ltEKP > article > div.eo2As > div.EtaWk > ul > li > div > button > span')
        },1500);
    page.on('response',async (response) => {
        if(response.url().startsWith("https://www.instagram.com/graphql/query/")){
            list.push(response.url());
            console.log(list);
            run();
        }
    });
    }
  )();
  async function run() {
    fetch(list[0])
    .then(res => res.json())
    .then(json => {
        console.log(json)
})
}

我们有这样的错误

https://prnt.sc/xzuact

您能告诉我如何使用 Nodejs 从该 URL 获取 JSON 数据吗?你认为的主要问题是什么?赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)