Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

api - What is the new instagram json endpoint?

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?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  • 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.

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,
...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...