我正试图将用户最近的Instagram媒体放在侧边栏上。我正在尝试使用Instagram API来获取媒体。
http://instagram.com/developer/endpoints/users/
文档说明了GET https://api.instagram.com/v1/users/<user-id>/media/recent/
,但它说要传递OAuth访问令牌。访问令牌代表代表用户行事的授权。我不希望用户登录Instagram以在侧栏上看到这一点。他们甚至不需要拥有Instagram帐户。
例如,我可以在没有登录Instagram并查看照片的情况下前往http://instagram.com/thebrainscoop。我想通过API来做到这一点。
在Instagram API中,未经用户身份验证的请求会传递client_id
而不是access_token
。但是,如果我尝试这样做,我会得到:
{
"meta":{
"error_type":"OAuthParameterException",
"code":400,
"error_message":"\"access_token\" URL parameter missing. This OAuth request requires an \"access_token\" URL parameter."
}
}
那么,这不可能吗?没有要求用户首先通过OAuth登录Instagram帐户,是否无法获取用户的最新(公共)媒体?
答案 0 :(得分:313)
var name = "smena8m";
$.get("https://images"+~~(Math.random()*33)+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https://www.instagram.com/" + name + "/", function(html) {
if (html) {
var regex = /_sharedData = ({.*);<\/script>/m,
json = JSON.parse(regex.exec(html)[1]),
edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges;
$.each(edges, function(n, edge) {
var node = edge.node;
$('body').append(
$('<a/>', {
href: 'https://instagr.am/p/'+node.shortcode,
target: '_blank'
}).css({
backgroundImage: 'url(' + node.thumbnail_src + ')'
}));
});
}
});
&#13;
html, body {
font-size: 0;
line-height: 0;
}
a {
display: inline-block;
width: 25%;
height: 0;
padding-bottom: 25%;
background: #eee 50% 50% no-repeat;
background-size: cover;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
您可以使用着陆页地址like this旁边的?__a=1
以JSON格式下载任何Instagram用户照片Feed。无需获取用户ID或注册应用程序,无需令牌,也无需oAuth。
min_id
和max_id
变量可用于分页,此处为example
YQL
可能无法在剪切的iframe中使用,因此您始终可以在YQL Console中手动检查
2018年4月更新:在最新的Instagram更新后,您无法在客户端(javascript)执行此操作,因为签名请求的自定义标头无法使用javascript设置CORS
{{1限制。仍然可以通过Access-Control-Allow-Headers
或任何其他服务器端方法执行此操作,并使用基于php
,rhx_gis
和请求参数的正确签名。您可以阅读更多相关信息here。
2019年1月更新:YQL已停用,因此,请将我的最新更新与Google Image Proxy一起检查为Instagram页面的csrf_token
代理!然后只有负面的时刻 - 这种方法没有分页。
CORS
解决方案:
PHP
答案 1 :(得分:122)
这已经很晚了,但是如果有人帮助我,我会在Instagram的文档中看不到它。
要在https://api.instagram.com/v1/users/<user-id>/media/recent/
上执行GET(目前正在撰写),您实际上不需要OAuth访问令牌。
您可以执行https://api.instagram.com/v1/users/[USER ID]/media/recent/?client_id=[CLIENT ID]
[客户ID]将是通过管理客户在应用程序中注册的有效客户端ID(与用户无关)。
您可以通过执行GET用户搜索请求从用户名获取[USER ID]:
https://api.instagram.com/v1/users/search?q=[USERNAME]&client_id=[CLIENT ID]
答案 2 :(得分:30)
<强>二○一七年十一月十一日强>
由于Instagram改变了他们提供这些数据的方式,现在上述方法都没有用。以下是获取用户媒体的新方法:
获取https://instagram.com/graphql/query/?query_id=17888483320059182&variables={"id":"1951415043","first":20,"after":null}
其中:
query_id
- 永久值: 17888483320059182 (请注意以后可能会更改。)
id
- 用户的ID。它可能带有用户列表。要获取用户列表,您可以使用以下请求:GET https://www.instagram.com/web/search/topsearch/?context=blended&query=YOUR_QUERY
first
- 要获得的物品数量
after
- 如果您想从该ID获取项目,则为最后一项的ID。
答案 3 :(得分:16)
截至上周,Instagram已停用/media/
网址,我实施了一种解决方法,目前效果非常好。
为了解决这个问题中的每个人的问题,我写了这个:https://github.com/whizzzkid/instagram-reverse-proxy
它使用以下端点提供所有Instagram的公共数据:
获取用户媒体:
https://igapi.ga/<username>/media
e.g.: https://igapi.ga/whizzzkid/media
获取具有限制计数的用户媒体:
https://igapi.ga/<username>/media?count=N // 1 < N < 20
e.g.: https://igapi.ga/whizzzkid/media?count=5
使用JSONP:
https://igapi.ga/<username>/media?callback=foo
e.g.: https://igapi.ga/whizzzkid/media?callback=bar
代理API还会将下一页和上一页网址附加到响应中,因此您无需在最后计算该内容。
希望你们喜欢它!
感谢@ 350D发现这一点:)
答案 4 :(得分:12)
Instagram API需要通过OAuth进行用户身份验证才能访问用户的最新媒体端点。现在似乎没有任何其他方式可以为用户获取所有媒体。
答案 5 :(得分:9)
如果您正在寻找一种生成访问令牌以在单个帐户上使用的方法,可以试试这个 - &gt; https://coderwall.com/p/cfgneq
我需要一种方法来使用instagram API来获取特定帐户的所有最新媒体。
答案 6 :(得分:9)
这是一个铁路解决方案。它是一种后门,实际上就是前门。
# create a headless browser
b = Watir::Browser.new :phantomjs
uri = 'https://www.instagram.com/explore/tags/' + query
uri = 'https://www.instagram.com/' + query if type == 'user'
b.goto uri
# all data are stored on this page-level object.
o = b.execute_script( 'return window._sharedData;')
b.close
您获得的对象取决于它是用户搜索还是标签搜索。我得到这样的数据:
if type == 'user'
data = o[ 'entry_data' ][ 'ProfilePage' ][ 0 ][ 'user' ][ 'media' ][ 'nodes' ]
page_info = o[ 'entry_data' ][ 'ProfilePage' ][ 0 ][ 'user' ][ 'media' ][ 'page_info' ]
max_id = page_info[ 'end_cursor' ]
has_next_page = page_info[ 'has_next_page' ]
else
data = o[ 'entry_data' ][ 'TagPage' ][ 0 ][ 'tag' ][ 'media' ][ 'nodes' ]
page_info = o[ 'entry_data' ][ 'TagPage' ][ 0 ][ 'tag' ][ 'media' ][ 'page_info' ]
max_id = page_info[ 'end_cursor' ]
has_next_page = page_info[ 'has_next_page' ]
end
然后我通过以下方式构建网址来获得另一页结果:
uri = 'https://www.instagram.com/explore/tags/' + query_string.to_s\
+ '?&max_id=' + max_id.to_s
uri = 'https://www.instagram.com/' + query_string.to_s + '?&max_id='\
+ max_id.to_s if type === 'user'
答案 7 :(得分:7)
感谢Instagram不断变化(以及可怕的设计)API架构,上述大部分内容将不再适用于2018年4月。
如果您使用https://www.instagram.com/username/?__a=1
方法直接查询其API,则以下是访问单个帖子数据的最新路径。
假设您返回的JSON
数据为$data
,您可以使用以下路径示例循环显示每个结果:
foreach ($data->graphql->user->edge_owner_to_timeline_media->edges as $item) {
$content_id = $item->node->id;
$date_posted = $item-node->taken_at_timestamp;
$comments = $item->node->edge_media_to_comment->count;
$likes = $item->node->edge_liked_by->count;
$image = $item->node->display_url;
$content = $item->node->edge_media_to_caption->edges[0]->node->text;
// etc etc ....
}
最近这一变化的主要内容是graphql
和edge_owner_to_timeline_media
。
对于非商业用户来说,他们似乎要杀掉这个API访问权限。 {{3}}中的客户尽可能充分利用它。
希望它对某人有所帮助;)
答案 8 :(得分:5)
只想加入@ 350D答案,因为我很难理解。
我的代码逻辑是下一个:
第一次调用API时,我只调用https://www.instagram.com/_vull_
/media/
。当我收到回复时,我检查more_available
的布尔值。如果是真的,我从数组中获取最后一张照片,获取其ID,然后再次调用Instagram API,但这次是https://www.instagram.com/_vull_/media/?max_id=1400286183132701451_1642962433
。
重要的是要知道,这个Id是数组中最后一张图片的Id。因此,当要求maxId使用数组中图片的最后一个id时,您将获得接下来的20张图片,依此类推。
希望这能澄清事情。
答案 9 :(得分:4)
如果您绕过Oauth,您可能不知道他们是哪个Instagram用户。话虽如此,有几种方法可以在没有身份验证的情况下获取Instagram图像。
Instagram的API允许您在不进行身份验证的情况下查看用户最受欢迎的图像。使用以下端点:Here is link
Instagram在this处为标签提供RSS订阅源。
Instagram用户页面是公开的,因此您可以使用PHP和CURL来获取他们的页面和DOM解析器来搜索html以获取您想要的图像标记。
答案 10 :(得分:3)
还有一个技巧,就是按标签搜索照片:
final String url = "connectioninfo";
final String username = "user";
final String password = "password";
try {
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT document_id, file_name, data FROM documents WHERE file_name like '%.doc'";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
String da_document_id = resultSet.getString(1);
String file_name = resultSet.getString(2);
File data = new File("c:\\databaseDoc.doc");
FileOutputStream fos = new FileOutputStream(data);
byte[] buffer = new byte[1];
InputStream is = resultSet.getBinaryStream(3);
try {
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("da_document_id= " + da_document_id);
System.out.println("file_name= " + file_name);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
其中:
GET https://www.instagram.com/graphql/query/?query_hash=3e7706b09c6184d5eafd8b032dbcf487&variables={"tag_name":"nature","first":25,"after":""}
- 永久价值(我相信它的散列17888483320059182,将来可以更改)
query_hash
- 标题不言自明
tag_name
- 要获取的项目数量(我不知道原因,但此值无法按预期工作。实际返回的照片数量略大于值乘以4.5(约为110)值为25,值为100)约为460))
first
- 如果您想从该ID获取项目,则为最后一项的ID。可以在此处使用来自JSON响应的after
的值。
答案 11 :(得分:3)
好吧,当/?__a=1
现在停止工作时,最好使用curl并解析Instagram页面,如下所示:Generate access token Instagram API, without having to log in?
答案 12 :(得分:2)
<强>更新强>
这种方法不再适用
使用Javascript:
$(document).ready(function(){
var username = "leomessi";
var max_num_items = 5;
var jqxhr = $.ajax( "https://www.instagram.com/"+username+"/?__a=1" ).done(function() {
//alert( "success" );
}).fail(function() {
//alert( "error" );
}).always(function(data) {
//alert( "complete" )
items = data.graphql.user.edge_owner_to_timeline_media.edges;
$.each(items, function(n, item) {
if( (n+1) <= max_num_items )
{
var data_li = "<li><a target='_blank' href='https://www.instagram.com/p/"+item.node.shortcode+"'><img src='" + item.node.thumbnail_src + "'/></a></li>";
$("ul.instagram").append(data_li);
}
});
});
});
HTML:
<ul class="instagram">
</ul>
CSS:
ul.instagram {
list-style: none;
}
ul.instagram li {
float: left;
}
ul.instagram li img {
height: 100px;
}
答案 13 :(得分:2)
您可以使用此API检索instagram用户的公共信息:
https://api.lityapp.com/instagrams/thebrainscoop?limit=2
如果您未设置limit参数,则默认情况下帖子数限制为12
如您在代码中所见,此api是在SpringBoot中使用HtmlUnit制作的:
public JSONObject getPublicInstagramByUserName(String userName, Integer limit) {
String html;
WebClient webClient = new WebClient();
try {
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getCookieManager().setCookiesEnabled(true);
Page page = webClient.getPage("https://www.instagram.com/" + userName);
WebResponse response = page.getWebResponse();
html = response.getContentAsString();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Ocorreu um erro no Instagram");
}
String prefix = "static/bundles/es6/ProfilePageContainer.js";
String sufix = "\"";
String script = html.substring(html.indexOf(prefix));
script = script.substring(0, script.indexOf(sufix));
try {
Page page = webClient.getPage("https://www.instagram.com/" + script);
WebResponse response = page.getWebResponse();
script = response.getContentAsString();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Ocorreu um erro no Instagram");
}
prefix = "l.pagination},queryId:\"";
String queryHash = script.substring(script.indexOf(prefix) + prefix.length());
queryHash = queryHash.substring(0, queryHash.indexOf(sufix));
prefix = "<script type=\"text/javascript\">window._sharedData = ";
sufix = ";</script>";
html = html.substring(html.indexOf(prefix) + prefix.length());
html = html.substring(0, html.indexOf(sufix));
JSONObject json = new JSONObject(html);
JSONObject entryData = json.getJSONObject("entry_data");
JSONObject profilePage = (JSONObject) entryData.getJSONArray("ProfilePage").get(0);
JSONObject graphql = profilePage.getJSONObject("graphql");
JSONObject user = graphql.getJSONObject("user");
JSONObject response = new JSONObject();
response.put("id", user.getString("id"));
response.put("username", user.getString("username"));
response.put("fullName", user.getString("full_name"));
response.put("followedBy", user.getJSONObject("edge_followed_by").getLong("count"));
response.put("following", user.getJSONObject("edge_follow").getLong("count"));
response.put("isBusinessAccount", user.getBoolean("is_business_account"));
response.put("photoUrl", user.getString("profile_pic_url"));
response.put("photoUrlHD", user.getString("profile_pic_url_hd"));
JSONObject edgeOwnerToTimelineMedia = user.getJSONObject("edge_owner_to_timeline_media");
JSONArray posts = new JSONArray();
try {
loadPublicInstagramPosts(webClient, queryHash, user.getString("id"), posts, edgeOwnerToTimelineMedia, limit == null ? 12 : limit);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Você fez muitas chamadas, tente mais tarde");
}
response.put("posts", posts);
return response;
}
private void loadPublicInstagramPosts(WebClient webClient, String queryHash, String userId, JSONArray posts, JSONObject edgeOwnerToTimelineMedia, Integer limit) throws IOException {
JSONArray edges = edgeOwnerToTimelineMedia.getJSONArray("edges");
for (Object elem : edges) {
if (limit != null && posts.length() == limit) {
return;
}
JSONObject node = ((JSONObject) elem).getJSONObject("node");
if (node.getBoolean("is_video")) {
continue;
}
JSONObject post = new JSONObject();
post.put("id", node.getString("id"));
post.put("shortcode", node.getString("shortcode"));
JSONArray captionEdges = node.getJSONObject("edge_media_to_caption").getJSONArray("edges");
if (captionEdges.length() > 0) {
JSONObject captionNode = ((JSONObject) captionEdges.get(0)).getJSONObject("node");
post.put("caption", captionNode.getString("text"));
} else {
post.put("caption", (Object) null);
}
post.put("photoUrl", node.getString("display_url"));
JSONObject dimensions = node.getJSONObject("dimensions");
post.put("photoWidth", dimensions.getLong("width"));
post.put("photoHeight", dimensions.getLong("height"));
JSONArray thumbnailResources = node.getJSONArray("thumbnail_resources");
JSONArray thumbnails = new JSONArray();
for (Object elem2 : thumbnailResources) {
JSONObject obj = (JSONObject) elem2;
JSONObject thumbnail = new JSONObject();
thumbnail.put("photoUrl", obj.getString("src"));
thumbnail.put("photoWidth", obj.getLong("config_width"));
thumbnail.put("photoHeight", obj.getLong("config_height"));
thumbnails.put(thumbnail);
}
post.put("thumbnails", thumbnails);
posts.put(post);
}
JSONObject pageInfo = edgeOwnerToTimelineMedia.getJSONObject("page_info");
if (!pageInfo.getBoolean("has_next_page")) {
return;
}
String endCursor = pageInfo.getString("end_cursor");
String variables = "{\"id\":\"" + userId + "\",\"first\":12,\"after\":\"" + endCursor + "\"}";
String url = "https://www.instagram.com/graphql/query/?query_hash=" + queryHash + "&variables=" + URLEncoder.encode(variables, "UTF-8");
Page page = webClient.getPage(url);
WebResponse response = page.getWebResponse();
String content = response.getContentAsString();
JSONObject json = new JSONObject(content);
loadPublicInstagramPosts(webClient, queryHash, userId, posts, json.getJSONObject("data").getJSONObject("user").getJSONObject("edge_owner_to_timeline_media"), limit);
}
这是一个响应示例:
{
"id": "290482318",
"username": "thebrainscoop",
"fullName": "Official Fan Page",
"followedBy": 1023,
"following": 6,
"isBusinessAccount": false,
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
"photoUrlHD": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
"posts": [
{
"id": "1430331382090378714",
"shortcode": "BPZjtBUly3a",
"caption": "If I have any active followers anymore; hello! I'm Brianna, and I created this account when I was just 12 years old to show my love for The Brain Scoop. I'm now nearly finished high school, and just rediscovered it. I just wanted to see if anyone is still active on here, and also correct some of my past mistakes - being a child at the time, I didn't realise I had to credit artists for their work, so I'm going to try to correct that post haste. Also; the font in my bio is horrendous. Why'd I think that was a good idea? Anyway, this is a beautiful artwork of the long-tailed pangolin by @chelsealinaeve . Check her out!",
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ab823331376ca46136457f4654bf2880/5CAD48E4/t51.2885-15/e35/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 640,
"photoHeight": 457,
"thumbnails": [
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/43b195566d0ef2ad5f4663ff76d62d23/5C76D756/t51.2885-15/e35/c91.0.457.457/s150x150/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 150,
"photoHeight": 150
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae39043a7ac050c56d741d8b4355c185/5C93971C/t51.2885-15/e35/c91.0.457.457/s240x240/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 240,
"photoHeight": 240
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae7a22d09e3ef98d0a6bbf31d621a3b7/5CACBBA6/t51.2885-15/e35/c91.0.457.457/s320x320/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 320,
"photoHeight": 320
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 480,
"photoHeight": 480
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 640,
"photoHeight": 640
}
]
},
{
"id": "442527661838057235",
"shortcode": "YkLJBXJD8T",
"caption": null,
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 612,
"photoHeight": 612,
"thumbnails": [
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/c1153c6513c44a6463d897e14b2d8f06/5CB13ADD/t51.2885-15/e15/s150x150/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 150,
"photoHeight": 150
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/47e60ec8bca5a1382cd9ac562439d48c/5CAE6A82/t51.2885-15/e15/s240x240/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 240,
"photoHeight": 240
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/da0ee5b666ab40e4adc1119e2edca014/5CADCB59/t51.2885-15/e15/s320x320/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 320,
"photoHeight": 320
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/02ee23571322ea8d0992e81e72f80ef2/5C741048/t51.2885-15/e15/s480x480/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 480,
"photoHeight": 480
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 640,
"photoHeight": 640
}
]
}
]
}
答案 14 :(得分:1)
以下nodejs代码从Instagram页面中删除流行的图像。 函数'ScrapeInstagramPage'负责老化后效应。
var request = require('parse5');
var request = require('request');
var rp = require('request-promise');
var $ = require('cheerio'); // Basically jQuery for node.js
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
function ScrapeInstagramPage (args) {
dout("ScrapeInstagramPage for username -> " + args.username);
var query_url = 'https://www.instagram.com/' + args.username + '/';
var cookieString = '';
var options = {
url: query_url,
method: 'GET',
headers: {
'x-requested-with' : 'XMLHttpRequest',
'accept-language' : 'en-US,en;q=0.8,pt;q=0.6,hi;q=0.4',
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'referer' : 'https://www.instagram.com/dress_blouse_designer/',
'Cookie' : cookieString,
'Accept' : '*/*',
'Connection' : 'keep-alive',
'authority' : 'www.instagram.com'
}
};
function dout (msg) {
if (args.debug) {
console.log(msg);
}
}
function autoParse(body, response, resolveWithFullResponse) {
// FIXME: The content type string could contain additional values like the charset.
// Consider using the `content-type` library for a robust comparison.
if (response.headers['content-type'] === 'application/json') {
return JSON.parse(body);
} else if (response.headers['content-type'] === 'text/html') {
return $.load(body);
} else {
return body;
}
}
options.transform = autoParse;
rp(options)
.then(function (autoParsedBody) {
if (args.debug) {
console.log("Responce of 'Get first user page': ");
console.log(autoParsedBody);
console.log("Creating JSDOM from above Responce...");
}
const dom = new JSDOM(autoParsedBody.html(), { runScripts: "dangerously" });
if (args.debug) console.log(dom.window._sharedData); // full data doc form instagram for a page
var user = dom.window._sharedData.entry_data.ProfilePage[0].user;
if (args.debug) {
console.log(user); // page user
console.log(user.id); // user ID
console.log(user.full_name); // user full_name
console.log(user.username); // user username
console.log(user.followed_by.count); // user followed_by
console.log(user.profile_pic_url_hd); // user profile pic
console.log(autoParsedBody.html());
}
if (user.is_private) {
dout ("User account is PRIVATE");
} else {
dout ("User account is public");
GetPostsFromUser(user.id, 5000, undefined);
}
})
.catch(function (err) {
console.log( "ERROR: " + err );
});
var pop_posts = [];
function GetPostsFromUser (user_id, first, end_cursor) {
var end_cursor_str = "";
if (end_cursor != undefined) {
end_cursor_str = '&after=' + end_cursor;
}
options.url = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id='
+ user_id + '&first=' + first + end_cursor_str;
rp(options)
.then(function (autoParsedBody) {
if (autoParsedBody.status === "ok") {
if (args.debug) console.log(autoParsedBody.data);
var posts = autoParsedBody.data.user.edge_owner_to_timeline_media;
// POSTS processing
if (posts.edges.length > 0) {
//console.log(posts.edges);
pop_posts = pop_posts.concat
(posts.edges.map(function(e) {
var d = new Date();
var now_seconds = d.getTime() / 1000;
var seconds_since_post = now_seconds - e.node.taken_at_timestamp;
//console.log("seconds_since_post: " + seconds_since_post);
var ageing = 10; // valuses (1-10]; big value means no ageing
var days_since_post = Math.floor(seconds_since_post/(24*60*60));
var df = (Math.log(ageing+days_since_post) / (Math.log(ageing)));
var likes_per_day = (e.node.edge_liked_by.count / df);
// console.log("likes: " + e.node.edge_liked_by.count);
//console.log("df: " + df);
//console.log("likes_per_day: " + likes_per_day);
//return (likes_per_day > 10 * 1000);
var obj = {};
obj.url = e.node.display_url;
obj.likes_per_day = likes_per_day;
obj.days_since_post = days_since_post;
obj.total_likes = e.node.edge_liked_by.count;
return obj;
}
));
pop_posts.sort(function (b,a) {
if (a.likes_per_day < b.likes_per_day)
return -1;
if (a.likes_per_day > b.likes_per_day)
return 1;
return 0;
});
//console.log(pop_posts);
pop_posts.forEach(function (obj) {
console.log(obj.url);
});
}
if (posts.page_info.has_next_page) {
GetPostsFromUser(user_id, first, posts.page_info.end_cursor);
}
} else {
console.log( "ERROR: Posts AJAX call not returned good..." );
}
})
.catch(function (err) {
console.log( "ERROR: " + err );
});
}
}
ScrapeInstagramPage ({username : "dress_blouse_designer", debug : false});
试试here
实施例: 对于给定的URL“https://www.instagram.com/dress_blouse_designer/”,可以调用函数
ScrapeInstagramPage ({username : "dress_blouse_designer", debug : false});
答案 15 :(得分:1)
除了Wordpress,我确实需要此功能。我适合并且效果很好
<script>
jQuery(function($){
var name = "caririceara.comcariri";
$.get("https://images"+~~(Math.random()*33)+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https://www.instagram.com/" + name + "/", function(html) {
if (html) {
var regex = /_sharedData = ({.*);<\/script>/m,
json = JSON.parse(regex.exec(html)[1]),
edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges;
$.each(edges, function(n, edge) {
if (n <= 7){
var node = edge.node;
$('.img_ins').append('<a href="https://instagr.am/p/'+node.shortcode+'" target="_blank"><img src="'+node.thumbnail_src+'" width="150"></a>');
}
});
}
});
});
</script>
答案 16 :(得分:0)
这可以使用简单的ajax调用和迭代图像路径。
var name = "nasa";
$.get("https://www.instagram.com/" + name + "/?__a=1", function (data, status) {
console.log('IG_NODES', data.user.media.nodes);
$.each(data.user.media.nodes, function (n, item) {
console.log('ITEMS', item.display_src);
$('body').append(
"<div class='col-md-4'><img class='img-fluid d-block' src='" + item.display_src + "'></div>"
);
});
})
答案 17 :(得分:0)
如果您要搜索没有clientID和访问令牌的用户:
1:如果要搜索名称与搜索词相似的所有用户:
用您要搜索的文本替换SeachName:
https://www.instagram.com/web/search/topsearch/?query=SearchName
2:如果要搜索完全相同的用户名:
用所需的搜索名称替换用户名:
答案 18 :(得分:-1)
这是一个PHP脚本,可下载图像并创建带有图像链接的html文件。信誉350D为php版本,这只是详细说明了。我建议您将其作为一项Cron工作,并根据需要经常解雇。 从2019年5月起已通过验证。
<?
$user = 'smena8m';
$igdata = file_get_contents('https://instagram.com/'.$user.'/');
preg_match('/_sharedData = ({.*);<\/script>/',$igdata,$matches);
$profile_data = json_decode($matches[1])->entry_data->ProfilePage[0]->graphql->user;
$html = '<div class="instagramBox" style="display:inline-grid;grid-template-columns:auto auto auto;">';
$i = 0;
$max = 9;
while($i<$max){
$imglink = $profile_data->edge_owner_to_timeline_media->edges[$i]->node->shortcode;
$img = $profile_data->edge_owner_to_timeline_media->edges[$i]->node->thumbnail_resources[0]->src;
file_put_contents('ig'.$i.'.jpg',file_get_contents($img));
$html .= '<a href="https://www.instagram.com/p/'.$imglink.'/" target="_blank"><img src="ig'.$i.'.jpg" /></a>';
$i++;
}
$html .= '</div>';
$instagram = fopen('instagram.html','w');
fwrite($instagram,$html);
fclose($instagram);
?>