Q:

Extracting Text from Javascript using BeautifulSoup to obtain number of followers

I am trying to automate the process of obtaining the number of followers for instagram for a few accounts. 
For example, https://www.instagram.com/taylorswift/ etc
I have tried to use libraries like BeautifulSoup.  However, one problem faced is that the number of followers is under a javascript type, and has many other text with it. 
My code would be:

>>> from bs4 import BeautifulSoup
>>> import requests
>>> url = "https://www.instagram.com/taylorswift/"
>>> page = requests.get(url)
>>> soup = BeautifulSoup(page.content)
>>> script = soup.select("script") 

May I ask how to obtain the number of followers as the only output? ( i.e 76.8m) I am new to python and scripting :/ Thank you. 

A:

this code works fine for me:
#! /usr/bin/env python

import requests
from json import loads

username = "liamgiannini"

r = requests.get('https://www.instagram.com/'+username)
html = r.text.encode("utf-8")
text = html[html.index("window._sharedData = ")+21:]
text = (text[:text.index("};</script>")]+"}").replace('\\"', "")
dictionary= loads(text)
data = dictionary["entry_data"]["ProfilePage"][0]["user"]

print "followers: "+str(data["followed_by"]["count"])

by printing print data.keys() you can access a lot more information about the user

