两天前使用cookie ig_pr
几天后就是阻止了。看起来现在获取数据的唯一方法是使用具有特定值的sessionid
我使用的是Instagram?__ a = 1 url来阅读Instagram用户的所有帖子。
几个小时前,响应发生了变化,现在不允许我使用max_id
进行分页。
在我通常发送请求之前
https://www.instagram.com/{{username}}/?__a=1
并在响应中使用graphql.edge_owner_to_timeline_media.page_info.end_cursor
我使用新的max_id
https://www.instagram.com/{{username}}/?__a=1&max_id={{end_cursor}}
现在每个通话中的end_cursor
更改& max_id不起作用。
请帮助:)
答案 0 :(得分:14)
编辑 15/03 不再工作 好像 instagram 再次更改了他们的 API,现在它给出了 CORS 错误。
截至 2021 年 2 月 2 日,我找到了解决方案
而不是使用 https://www.instagram.com/username/?__a=1 它要求登录。
只添加一个 /channel 似乎就可以了,就像这样:
答案 1 :(得分:12)
query_hash至少在过去几天内没有变化。它表示它是什么类型的查询。
下面列出了我认识的4种查询类型,希望这些帮助。
在https://www.instagram.com/someone/?__a=1
下加载更多媒体https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id":"93024","first":12,"after":"XXXXXXXX"}
(Instagram阻止了自2018-04-12以来的上述访问。您必须删除__a = 1并在块中提取JSON。在HTML中查找" window._sharedData"
在https://www.instagram.com/explore/tags/iphone/?__a=1
下加载更多媒体https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables={"tag_name":"iphone","first":12,"after":"XXXXXXXX"}
在https://www.instagram.com/explore/locations/703629436462521/?__a=1
下加载更多媒体https://www.instagram.com/graphql/query/?query_hash=ac38b90f0f3981c42092016a37c59bf7&variables={"id":"703629436462521","first":12,"after":"XXXXXXXX"}
为https://www.instagram.com/p/Bf-I2P6grhd/
加载更多评论https://www.instagram.com/graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={"shortcode":"Bf-I2P6grhd","first":20,"after":"XXXXXXXX"}
其中XXXXXXXX是原始请求的end_cursor
答案 2 :(得分:10)
我刚刚遇到同样的问题。
看起来他们只是通过删除 max_id 来更改其私人API。 他们的网站似乎用graphql api替换了旧的api。
https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables= { “ID”: “111”, “第一”:12, “后”: “XXX”}
使用query_hash或query_id
我不知道query_id / query_hash会工作多久,这取决于Instagram。他们最终会改变它。
更新于4/8/2018 - 在FB未检查任何Cookie之前,但看起来他们添加了快速验证。在发送API时,请尝试将ig_pr=2
添加到请求Cookie。这个快速修复对我有用。谁知道FB什么时候会改变它。
只要FB不强制登录这些基本API,总会有一个简单的解决方法。
答案 3 :(得分:7)
使用图表/查询的主要问题是我只有用户名,以提取userId&像我们使用的最后一篇文章?__ a = 1我们必须得到Instagram的用户页面&提取_sharedData
<强>的Javascript 强>
let url = "https://www.instagram.com/"+username;
$.ajax({
type: 'GET',
url: url,
error: function () {
//..
},
success: function (data) {
data = JSON.parse(data.split("window._sharedData = ")[1].split(";</script>")[0]).entry_data.ProfilePage[0].graphql;
console.log(data);
}
})
获取所有这些数据后,我们可以调用图形/查询(不在客户端)
答案 4 :(得分:6)
翻译了一些人&#39;代码到PHP:
<?php
function getPublicInfo($username) {
$url = sprintf("https://www.instagram.com/$username");
$content = file_get_contents($url);
$content = explode("window._sharedData = ", $content)[1];
$content = explode(";</script>", $content)[0];
$data = json_decode($content, true);
return $data['entry_data']['ProfilePage'][0];
}
不确定它会工作多久。对于我的小项目,它现在可以完成这项工作。结果与URL上的结果非常相似(如果不相等):instagram.com/{user}/?__a=1
答案 5 :(得分:3)
这个答案不是直接帮助问题而是发布,因为有人可能会从答案中受益。截至2018年4月12日的当前日期,如果没有设置Cookie
标头,则加载更多API将无法运行。
以下是一些获取Instagram公共APIS的代码
let url = "https://www.instagram.com/explore/";
if (payload.type == 'location') {
url = url + "locations/" + payload.location_id + "/" + payload.location_name + "/?__a=1";
} else if (payload.type == 'hashtag') {
url = url + "tags/" + payload.hashtag + "/?__a=1";
} else { //profile
url = "https://www.instagram.com/" + payload.user_name + "/?__a=1";
}
request(url, function (error, response, body) {
body = JSON.parse(body);
//below are params which are required for load more pagination payload
paginationData = {
has_next_page: body.data.user.edge_owner_to_timeline_media.page_info.has_next_page,
end_cursor: body.data.user.edge_owner_to_timeline_media.page_info.end_cursor
};
//user.edge_owner_to_timeline_media for profile posts,
//hashtag.edge_hashtag_to_media for hashtag posts
//location.edge_location_to_media for location posts
});
并且为了加载更多项目,我正在使用:
let url = "https://www.instagram.com/graphql/query/";
if (payload.type == 'location') {
let variables = encodeURIComponent('{"id":"' + payload.pagination.id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
url = url + "?query_hash=ac38b90f0f3981c42092016a37c59bf7&query_id=17865274345132052&variables=" + variables;
} else if (payload.type == 'hashtag') {
let variables = encodeURIComponent('{"tag_name":"' + payload.pagination.tag_name + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
url = url + "?query_hash=298b92c8d7cad703f7565aa892ede943&query_id=17875800862117404&variables=" + variables;
} else { //profile
let variables = encodeURIComponent('{"id":"' + payload.pagination.owner_id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
url = url + "?query_hash=472f257a40c653c64c666ce877d59d2b&query_id=17888483320059182&variables=" + variables;
}
let options = {
url: url,
headers: {
Cookie: "Cookie value which i copied from my logged in instagram browser window"
}
};
request(options, function (error, response, body) { });
似乎不再需要query_id
,现在query_hash
就足够了。我不确定但是对我来说它似乎没有它们。
答案 6 :(得分:1)
对于分页,您现在可以使用?__ a = 1&amp; page = 2
答案 7 :(得分:1)
截至2018年4月12日下午4:00(GMT + 1),API查询无需任何Cookie即可运行。我不知道他们在做什么......
在私人导航中试试this link。
答案 8 :(得分:1)
自 2021 年 5 月 21 日起,使用 /channel 将使其工作,但前提是在您的请求中使用浏览器 User-Agent 标头,例如使用 curl:
curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" https://www.instagram.com/{username}/channel/?__a=1
答案 9 :(得分:1)
我遇到了类似的问题,因为我无法使用“?__a=1”解析 JSON 文件,结果以 JSONDecodeError: Expecting value
结束。找了很多地方终于找到了,用Header解决了问题。尝试使用这个,它对我有用
link = 'http://instagram.com/instagram/?__a=1'
headers = {'User-Agent': 'Mozilla'}
r = requests.get(link, headers=headers)
data = r.json()