什么是新的 instagram json 端点? [英] What is the new instagram json endpoint?

查看:23
本文介绍了什么是新的 instagram json 端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Instagram 曾经在端点 https://www.instagram.com//?__a=1 下将开放数据公开为 json.这在一夜之间发生了变化,端点不再可用.什么是新端点或可以替代什么?

Instagram used to expose open data as json under the endpoint https://www.instagram.com/<username>/?__a=1. This changed over night, the endpoint is not available anymore. What is the new endpoint or what could be an alternative to this?

提前致谢!

推荐答案

端点不再存在.由于丑闻,Facebook 正在限制 API.数据当然还在那里,Instagram 的前端需要它,所以现在的替代方法是抓取页面并在那里找到 json 数据.这是我的做法:

The endpoint does not exist anymore. Facebook is restricting APIs because of scandals. The data is still there of course, Instagram's frontend needs it, so the alternative right now is to scrape the page and find the json data there. Here is how I do it:

  • https://www.instagram.com/ 执行 http 访问.
  • 查找文本以 window._sharedData = 开头的 script 标记.您可以为此使用正则表达式或抓取库.
  • 剩下的文字(除了最后的;)就是你想要的json数据.
  • 将字符串化的 json 转换为 json 以便像以前一样访问它.
  • 'entry_data' 键中的 'ProfilePage' 键中的第一个元素与旧端点返回的 json 完全对应.
  • Do an http get to to https://www.instagram.com/<username>.
  • Look for the script tag which text's starts with window._sharedData =. You can use regular expressions or a scraping library for this.
  • The rest of the text (except for the ; at the end) is the json data you want.
  • Cast the stringified json into json in order to access it like before.
  • The first element in the 'ProfilePage' key in the 'entry_data' key corresponds exactly to the json returned by the old endpoint.

以下是使用 Python 的示例:

Here is an example using Python:

import requests
from bs4 import BeautifulSoup
import re
import json

r = requests.get('https://www.instagram.com/github/')
soup = BeautifulSoup(r.content)
scripts = soup.find_all('script', type="text/javascript", text=re.compile('window._sharedData'))
stringified_json = scripts[0].get_text().replace('window._sharedData = ', '')[:-1]

json.loads(stringified_json)['entry_data']['ProfilePage'][0]

Out[1]:
{u'graphql': {u'user': {u'biography': u'How people build software.',
u'blocked_by_viewer': False,
...
}

这篇关于什么是新的 instagram json 端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆