act*_*ner 30 instagram instagram-api
我想知道如何检索用户在Instagram上关注的人员列表.这是因为这个特定用户是我关注的人.所以我可以在Instagram应用程序上访问他/她的照片和他的粉丝.
我如何使用Instagram API执行此操作?这合法吗?
Jea*_*sta 87
/sf/answers/4413957621/的进一步开发版本
const username = "USER_NAME_HERE";
/**
* Initialized like this so we can still run it from browsers, but also use typescript on a code editor for intellisense.
*/
let followers = [{ username: "", full_name: "" }];
let followings = [{ username: "", full_name: "" }];
let dontFollowMeBack = [{ username: "", full_name: "" }];
let iDontFollowBack = [{ username: "", full_name: "" }];
followers = [];
followings = [];
dontFollowMeBack = [];
iDontFollowBack = [];
(async () => {
try {
console.log(`Process started! Give it a couple of seconds`);
const userQueryRes = await fetch(
`https://www.instagram.com/web/search/topsearch/?query=${username}`
);
const userQueryJson = await userQueryRes.json();
const userId = userQueryJson.users.map(u => u.user)
.filter(
u => u.username === username
)[0].pk;
let after = null;
let has_next = true;
while (has_next) {
await fetch(
`https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables=` +
encodeURIComponent(
JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after,
})
)
)
.then((res) => res.json())
.then((res) => {
has_next = res.data.user.edge_followed_by.page_info.has_next_page;
after = res.data.user.edge_followed_by.page_info.end_cursor;
followers = followers.concat(
res.data.user.edge_followed_by.edges.map(({ node }) => {
return {
username: node.username,
full_name: node.full_name,
};
})
);
});
}
console.log({ followers });
after = null;
has_next = true;
while (has_next) {
await fetch(
`https://www.instagram.com/graphql/query/?query_hash=d04b0a864b4b54837c0d870b0e77e076&variables=` +
encodeURIComponent(
JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after,
})
)
)
.then((res) => res.json())
.then((res) => {
has_next = res.data.user.edge_follow.page_info.has_next_page;
after = res.data.user.edge_follow.page_info.end_cursor;
followings = followings.concat(
res.data.user.edge_follow.edges.map(({ node }) => {
return {
username: node.username,
full_name: node.full_name,
};
})
);
});
}
console.log({ followings });
dontFollowMeBack = followings.filter((following) => {
return !followers.find(
(follower) => follower.username === following.username
);
});
console.log({ dontFollowMeBack });
iDontFollowBack = followers.filter((follower) => {
return !followings.find(
(following) => following.username === follower.username
);
});
console.log({ iDontFollowBack });
console.log(
`Process is done: Type 'copy(followers)' or 'copy(followings)' or 'copy(dontFollowMeBack)' or 'copy(iDontFollowBack)' in the console and paste it into a text editor to take a look at it'`
);
} catch (err) {
console.log({ err });
}
})();
Run Code Online (Sandbox Code Playgroud)
Mar*_*ägi 31
湿婆的回答不再适用.Instagram不支持API调用" / users/{user-id}/follow "一段时间(2016年被禁用).
有一段时间你只能通过"/ users/self/follow"端点获得你自己的粉丝/ 粉丝,但是Instagram在2018年4月禁用了该功能(带有Cambridge Analytica问题).你可以在这里阅读它.
据我所知(目前),没有可用的服务(官方或非官方),您可以获得用户的关注者/关注者(甚至是您自己的).
Cai*_*ris 25
这是一种仅使用浏览器和一些复制粘贴即可获取用户关注的人员列表的方法(基于 Deep Seeker 答案的纯 JavaScript 解决方案):
获取用户的 id(在浏览器中,导航到https://www.instagram.com/user_name/?__a=1并寻找 response -> graphql -> user -> id [来自 Deep Seeker 的回答])
打开另一个浏览器窗口
打开浏览器控制台并将其粘贴到其中
options = {
userId: your_user_id,
list: 1 //1 for following, 2 for followers
}Run Code Online (Sandbox Code Playgroud)
更改为您的用户 ID 并按 Enter
将此粘贴到控制台中并按 Enter
`https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables=` + encodeURIComponent(JSON.stringify({
"id": options.userId,
"include_reel": true,
"fetch_mutual": true,
"first": 50
}))Run Code Online (Sandbox Code Playgroud)
导航到输出的链接
(这会为 http 请求设置标头。如果您尝试在未打开的页面上运行脚本,它将无法工作。)
let config = {
followers: {
hash: 'c76146de99bb02f6415203be841dd25a',
path: 'edge_followed_by'
},
following: {
hash: 'd04b0a864b4b54837c0d870b0e77e076',
path: 'edge_follow'
}
};
var allUsers = [];
function getUsernames(data) {
var userBatch = data.map(element => element.node.username);
allUsers.push(...userBatch);
}
async function makeNextRequest(nextCurser, listConfig) {
var params = {
"id": options.userId,
"include_reel": true,
"fetch_mutual": true,
"first": 50
};
if (nextCurser) {
params.after = nextCurser;
}
var requestUrl = `https://www.instagram.com/graphql/query/?query_hash=` + listConfig.hash + `&variables=` + encodeURIComponent(JSON.stringify(params));
var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
var res = JSON.parse(xhr.response);
var userData = res.data.user[listConfig.path].edges;
getUsernames(userData);
var curser = "";
try {
curser = res.data.user[listConfig.path].page_info.end_cursor;
} catch {
}
var users = [];
if (curser) {
makeNextRequest(curser, listConfig);
} else {
var printString =""
allUsers.forEach(item => printString = printString + item + "\n");
console.log(printString);
}
}
xhr.open("GET", requestUrl);
xhr.send();
}
if (options.list === 1) {
console.log('following');
makeNextRequest("", config.following);
} else if (options.list === 2) {
console.log('followers');
makeNextRequest("", config.followers);
}Run Code Online (Sandbox Code Playgroud)
几秒钟后,它应该输出您的用户正在关注的用户列表。
编辑 3/12/2021
故障排除
如果您收到未兑现的承诺,请仔细检查这些事项
检查问题的一种方法是确保您在步骤 6 中导航到的页面有数据。如果看起来如下所示,那么您要么未登录,该用户是私人用户且您无权查看他们的关注者/关注者,要么您的浏览器不允许使用 cookie 且 Instagram 无法确认您的身份:
{"data":{"user":{"edge_followed_by":{"count":196,"page_info":{"has_next_page":false,"end_cursor":null},"edges":[]},"edge_mutual_followed_by":{"count":0,"edges":[]}}},"status":"ok"}Run Code Online (Sandbox Code Playgroud)
Shi*_*iva 14
您可以使用以下Instagram API端点来获取用户关注的人员列表.
https://api.instagram.com/v1/users/{user-id}/follows?access_token=ACCESS-TOKEN
这是该端点的完整文档.GET /用户/用户ID /如下
由于此端点需要user-id(而不是user-name),具体取决于您编写API客户端的方式,您可能必须使用用户名调用/ users/search端点,然后从响应中获取用户ID,将其传递给上面的/users/user-id/follows端点以获取关注者列表.
IANAL,但考虑到它在API中的记录,并查看使用条款,我不明白这是不合法的.
Chh*_*eng 14
我根据凯特琳·莫里斯(Caitlin Morris)在 Instagram 上获取所有关注者和关注者的回答,开创了自己的道路。只需复制此代码,粘贴到浏览器控制台并等待几秒钟。
您需要使用instagram.com选项卡中的浏览器控制台才能使其正常工作。
let username = 'USERNAME'
let followers = [], followings = []
try {
let res = await fetch(`https://www.instagram.com/${username}/?__a=1`)
res = await res.json()
let userId = res.graphql.user.id
let after = null, has_next = true
while (has_next) {
await fetch(`https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables=` + encodeURIComponent(JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after
}))).then(res => res.json()).then(res => {
has_next = res.data.user.edge_followed_by.page_info.has_next_page
after = res.data.user.edge_followed_by.page_info.end_cursor
followers = followers.concat(res.data.user.edge_followed_by.edges.map(({node}) => {
return {
username: node.username,
full_name: node.full_name
}
}))
})
}
console.log('Followers', followers)
has_next = true
after = null
while (has_next) {
await fetch(`https://www.instagram.com/graphql/query/?query_hash=d04b0a864b4b54837c0d870b0e77e076&variables=` + encodeURIComponent(JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after
}))).then(res => res.json()).then(res => {
has_next = res.data.user.edge_follow.page_info.has_next_page
after = res.data.user.edge_follow.page_info.end_cursor
followings = followings.concat(res.data.user.edge_follow.edges.map(({node}) => {
return {
username: node.username,
full_name: node.full_name
}
}))
})
}
console.log('Followings', followings)
} catch (err) {
console.log('Invalid username')
}
Run Code Online (Sandbox Code Playgroud)
小智 7
过去几天我一直在为 chrome 开发一些 Instagram 扩展,我得到了这个锻炼:
首先,您需要知道如果用户个人资料是公开的或者您已登录并且您正在关注该用户,则这可以工作。
我不确定为什么它会这样工作,但可能在您登录时设置了一些 cookie,并在获取私人配置文件时在后端检查这些 cookie。
现在我将与您分享一个 ajax 示例,但如果您不使用 jquery,您可以找到其他更适合您的示例。
此外,您可以注意到我们有两个 query_hash 值用于关注者和关注者以及其他查询不同的值。
let config = {
followers: {
hash: 'c76146de99bb02f6415203be841dd25a',
path: 'edge_followed_by'
},
followings: {
hash: 'd04b0a864b4b54837c0d870b0e77e076',
path: 'edge_follow'
}
};
Run Code Online (Sandbox Code Playgroud)
你可以从用户IDhttps://www.instagram.com/user_name/?__a=1为response.graphql.user.id
之后只是您收到的第一部分用户的响应,因为每个请求限制为 50 个用户:
let after = response.data.user[list].page_info.end_cursor
let data = {followers: [], followings: []};
function getFollows (user, list = 'followers', after = null) {
$.get(`https://www.instagram.com/graphql/query/?query_hash=${config[list].hash}&variables=${encodeURIComponent(JSON.stringify({
"id": user.id,
"include_reel": true,
"fetch_mutual": true,
"first": 50,
"after": after
}))}`, function (response) {
data[list].push(...response.data.user[config[list].path].edges);
if (response.data.user[config[list].path].page_info.has_next_page) {
setTimeout(function () {
getFollows(user, list, response.data.user[config[list].path].page_info.end_cursor);
}, 1000);
} else if (list === 'followers') {
getFollows(user, 'followings');
} else {
alert('DONE!');
console.log(followers);
console.log(followings);
}
});
}
Run Code Online (Sandbox Code Playgroud)
您可能可以在 Instagram 网站之外使用它,但我没有尝试过,您可能需要一些标题来匹配 Instagram 页面上的标题。
如果您需要这些标头的一些额外数据,您可能会在window._sharedData来自后端的带有 csrf 令牌等的 JSON 中找到这些数据。
您可以使用以下方法捕获此问题:
let $script = JSON.parse(document.body.innerHTML.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1));
Run Code Online (Sandbox Code Playgroud)
那都是我的!
希望能帮到你!
| 归档时间: |
|
| 查看次数: |
79039 次 |
| 最近记录: |