Search code examples
pythonjsonapiweb-scrapingeconomics

Accessing Json data through api with Python


I'm trying to access the Atlas Observatory of Economic Complexity API at this location: http://atlas.media.mit.edu/about/api/data/

Using the following code

import pandas as pd
import numpy as np
import matplotlib as mpl
from urllib2 import urlopen
import csv as csv
import json


url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
mydata=open(urllib2.urlopen(url))
response = json.loads(mydata)

To which I get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-fbe696d9098d> in <module>()
      8 
      9 url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
---> 10 mydata=open(urlopen(url))
     11 response = json.loads(mydata)
     12 

TypeError: coercing to Unicode: need string or buffer, instance found

I'm new to accessing data from API's via python so there may be something simple I am missing. The goal is to get trade data and put it into a pandas data frame. As a bonus if anyone has any good sources for working with APIs in python please let me know.


Solution

  • I recommend using requests, http://docs.python-requests.org/en/latest/. It can be installed via pip and is a very clean interface built on top of urllib2.

    import requests
    url = "http://atlas.media.mit.edu/hs/export/2010/show/all/all/"
    response = requests.get(url)
    mydata = response.text