Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 581
Diego Mora Cespedes Avatar asked Apr 12 '18 05:04

Diego Mora Cespedes


3 Answers

You can create a session like instagram-scraper package does.

You don't need to provide a username and password. Below snippet will create an anonymous session.

import requests
import json
try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

BASE_URL = 'https://www.instagram.com/'
CHROME_WIN_UA = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'

session = requests.Session()
session.headers = {'user-agent': CHROME_WIN_UA, 'Referer': BASE_URL}
session.cookies.set('ig_pr', '1')
req = session.get(BASE_URL)
session.headers.update({'X-CSRFToken': req.cookies['csrftoken']})

url = "https://www.instagram.com/instagram/?__a=1"
response = session.get(url, cookies="", headers={'Host': urlparse(url).hostname}, stream=False, timeout=90)
print(response.json())

https://github.com/arc298/instagram-scraper

like image 195
marty Avatar answered Sep 18 '22 02:09

marty


In case you're seeking for the regex:

<script type="text\/javascript">window[.]_sharedData = {[\s\S]*};<\/script>

like image 23
Emixam23 Avatar answered Sep 21 '22 02:09

Emixam23


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,
...
}
like image 22
Diego Mora Cespedes Avatar answered Sep 20 '22 02:09

Diego Mora Cespedes