为什么不在 NodeJS 中使用 API 就无法获取所有 Instagram 帖子
Why can't I get all Instagram posts without using API in NodeJS
请注意,截至 2020 年 3 月的今天,Instagram 已经发生了很大变化,因此,这个问题与之前的问题不同。
问题是我总是收到 12 个帖子,而不是所有使用非 Instagram-API 方式的帖子。我遇到过很多解决方案,但现在所有这些都只有 return 12 个帖子,甚至是著名的:
https://www.instagram.com/instagram/?__a=1
这是我使用 instagram 帐户得到的,该帐户有 6282 个帖子:
我也尝试过以下技巧,但它也 returns 12 个帖子:
async function instagramPhotos () {
// It will contain our photos' links
const res = []
try {
const userInfoSource = await Axios.get('https://www.instagram.com/instagram/')
// userInfoSource.data contains the HTML from Axios
const jsonObject = userInfoSource.data.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1)
const userInfo = JSON.parse(jsonObject)
// Retrieve only the first 10 results
const mediaArray = userInfo.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges
for (let media of mediaArray) {
const node = media.node
// Process only if is an image
if ((node.__typename && node.__typename !== 'GraphImage')) {
continue
}
// Push the thumbnail src in the array
res.push(node.thumbnail_src)
}
} catch (e) {
console.error('Unable to retrieve photos. Reason: ' + e.toString())
}
return res
}
有很多 npm 模块声称他们可以在不使用 Instagram 的情况下获得提要 API 但最后他们只有 return 12 个帖子,例如:(instagram-nodejs-without-api)
我运行下面的代码,我也只得到12个帖子:
let Instagram = require('instagram-nodejs-without-api');
Instagram = new Instagram()
const username="instagram"
const password="123hola"
Instagram.getCsrfToken().then((csrf) =>
{
Instagram.csrfToken = csrf;
}).then(() =>
{
return Instagram.auth(username, password).then(sessionId =>
{
Instagram.sessionId = sessionId
return Instagram.getUserDataByUsername(username).then((t) =>
{
console.log(t)
})
})
}).catch(console.error);
我的下一步是对 Instagram 个人资料执行网络抓取,但它非常慢,特别是当图像大约为 6000 时,是否有任何优雅的方法来获取这些帖子?我只想得到我的帖子,所以我不介意登录。
因为我需要从具体的个人资料中获取帖子,所以我在 chrome 浏览器中打开了个人资料,打开控制台并运行以下代码来获取所有帖子:
//LOOPING ALL (credit www.hamzadiaz.com)
let posts = []
setInterval(()=>{
var inputs = document.getElementsByClassName('v1Nh3 kIKUG _bz0w');
for(var i=0; i<inputs.length;i++) {
if(!posts.includes(inputs[i].getElementsByTagName("a")[0].href)){
posts.push(inputs[i].getElementsByTagName("a")[0].href)
}
}
},500);
请注意,截至 2020 年 3 月的今天,Instagram 已经发生了很大变化,因此,这个问题与之前的问题不同。 问题是我总是收到 12 个帖子,而不是所有使用非 Instagram-API 方式的帖子。我遇到过很多解决方案,但现在所有这些都只有 return 12 个帖子,甚至是著名的:
https://www.instagram.com/instagram/?__a=1
这是我使用 instagram 帐户得到的,该帐户有 6282 个帖子:
我也尝试过以下技巧,但它也 returns 12 个帖子:
async function instagramPhotos () {
// It will contain our photos' links
const res = []
try {
const userInfoSource = await Axios.get('https://www.instagram.com/instagram/')
// userInfoSource.data contains the HTML from Axios
const jsonObject = userInfoSource.data.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1)
const userInfo = JSON.parse(jsonObject)
// Retrieve only the first 10 results
const mediaArray = userInfo.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges
for (let media of mediaArray) {
const node = media.node
// Process only if is an image
if ((node.__typename && node.__typename !== 'GraphImage')) {
continue
}
// Push the thumbnail src in the array
res.push(node.thumbnail_src)
}
} catch (e) {
console.error('Unable to retrieve photos. Reason: ' + e.toString())
}
return res
}
有很多 npm 模块声称他们可以在不使用 Instagram 的情况下获得提要 API 但最后他们只有 return 12 个帖子,例如:(instagram-nodejs-without-api)
我运行下面的代码,我也只得到12个帖子:
let Instagram = require('instagram-nodejs-without-api');
Instagram = new Instagram()
const username="instagram"
const password="123hola"
Instagram.getCsrfToken().then((csrf) =>
{
Instagram.csrfToken = csrf;
}).then(() =>
{
return Instagram.auth(username, password).then(sessionId =>
{
Instagram.sessionId = sessionId
return Instagram.getUserDataByUsername(username).then((t) =>
{
console.log(t)
})
})
}).catch(console.error);
我的下一步是对 Instagram 个人资料执行网络抓取,但它非常慢,特别是当图像大约为 6000 时,是否有任何优雅的方法来获取这些帖子?我只想得到我的帖子,所以我不介意登录。
因为我需要从具体的个人资料中获取帖子,所以我在 chrome 浏览器中打开了个人资料,打开控制台并运行以下代码来获取所有帖子:
//LOOPING ALL (credit www.hamzadiaz.com)
let posts = []
setInterval(()=>{
var inputs = document.getElementsByClassName('v1Nh3 kIKUG _bz0w');
for(var i=0; i<inputs.length;i++) {
if(!posts.includes(inputs[i].getElementsByTagName("a")[0].href)){
posts.push(inputs[i].getElementsByTagName("a")[0].href)
}
}
},500);