Avi
Avi

Reputation: 1845

Unable to extract the table from API using python

I am trying to extract a table using an API but I am unable to do so. I am pretty sure that I am not using it correctly, and any help would be appreciated.

Actually I am trying to extract a table from this API but unable to figure out the right way on how to do it. This is what is mentioned in the website. I want to extract Latest_full_data table.

enter image description here

This is my code to get the table but I am getting error:

import urllib
import requests
import urllib.request

locu_api = 'api_Key'

def locu_search(query):
    api_key = locu_api
    url = 'https://www.quandl.com/api/v3/databases/WIKI/metadata?api_key=' + api_key
    response = urllib.request.urlopen(url).read()
    json_obj = str(response, 'utf-8')
    datanew = json.loads(json_obj)
    return datanew

When I do print(datanew). Update: Even if I change it to return data new, error is still the same. I am getting this below error: name 'datanew' is not defined

Upvotes: 1

Views: 923

Answers (2)

dben
dben

Reputation: 439

I had the same issues with urrlib before. If possible, try to use requests it's a better designed and working library in my opinion. Also, it is capable of reading JSON with a single function so no need to run it through multiple lines Sample code here:

import requests

locu_api = 'api_Key'

def locu_search():
   url = 'https://www.quandl.com/api/v3/databases/WIKI/metadata?api_key=' + api_key
   return requests.get(url).json()

locu_search()

Edit: The endpoint that you are calling might not be the correct one. I think you are looking for the following one:

import requests

api_key = 'your_api_key_here'

def locu_search(dataset_code):
   url = f'https://www.quandl.com/api/v3/datasets/WIKI/{dataset_code}/metadata.json?api_key={api_key}'
   req = requests.get(url)
   return req.json()

data = locu_search("FB")

This will return with all the metadata regarding a company. In this case Facebook.

Upvotes: 2

xerox_04
xerox_04

Reputation: 9

Maybe it doesn't apply to your specific problem, but what I normally do is the following:

import requests

def get_values(url):
   response = requests.get(url).text
   values = json.loads(response)
   return values

Upvotes: 0

Related Questions