#!/usr/bin/env python # coding: utf-8 # # Getting Data through an API call # So that was the hard way to get data. Now, lets access rap lyrics the easy way using an api that was written for rap genius, courtesy Ed Forson, you can read more about it http://genius-api.com/ # # In[ ]: import urllib import urllib2 import json import pprint #for pretty print of json files url = 'http://genius-api.com/api/artistInfo' values = {'name' : 'Gza', 'genre' : 'rap'} data = urllib.urlencode(values) req = urllib2.Request(url, data) # In[ ]: response = urllib2.urlopen(req) # In[ ]: the_page = response.read() # In[ ]: decoded_data = json.loads(the_page) # In[ ]: print json.dumps(decoded_data, sort_keys=True, indent=4) # In[ ]: decoded_data.viewkeys() # In[ ]: print json.dumps(decoded_data.get('songs'), sort_keys=True, indent=4) # In[ ]: song_url = 'http://genius-api.com/api/songInfo' #Song information artist_url = 'http://genius-api.com/api/artistInfo' #artist info values = {'name' : 'Liquid Swords', 'genre' : 'rap'} # In[ ]: data = urllib.urlencode(values) req = urllib2.Request(song_url, data) # In[ ]: response = urllib2.urlopen(req) # In[ ]: song_info = response.read() # In[ ]: decoded_data = json.loads(song_info) # In[ ]: print json.dumps(decoded_data, sort_keys=True, indent=4) # In[ ]: lyric_url = 'http://genius-api.com/api/lyricsInfo' parameters = {'link':'http://rapgenius.com/Gza-liquid-swords-lyrics', 'genre' : 'rap'} data = urllib.urlencode(parameters) # In[ ]: req = urllib2.Request(lyric_url, data) response = urllib2.urlopen(req) lyric_info = response.read() decoded_data = json.loads(lyric_info) # In[ ]: print json.dumps(decoded_data, sort_keys=True, indent=4) # In[ ]: decoded_data.viewkeys() # In[ ]: print json.dumps(decoded_data.get('lyrics'), sort_keys=True, indent=4) # In[ ]: Data_under_investigation = decoded_data.get('lyrics') # In[ ]: Data_under_investigation.viewkeys() # In[ ]: Data_under_investigation = decoded_data.get('lyrics').get('sections') # In[ ]: print json.dumps(Data_under_investigation, sort_keys=True, indent=4) # In[ ]: len(Data_under_investigation) # In[ ]: print json.dumps(Data_under_investigation[0].get('verses')[0].get('content'), sort_keys=True, indent=4) # In[ ]: print Data_under_investigation[1].get('verses')[0].get('content') # In[ ]: len(Data_under_investigation)