import urllib.request import urllib.response from urllib.error import HTTPError import json import xmltodict from os.path import join class APIRetrieve: def __init__(self,startswith_url,options_dict): #self.options = "&".join(myList) self.options = [] self.data = {} options_list = [] for key in options_dict: options_list.append("%s=%s" % (key,options_dict[key])) options = "&".join(options_list) self.full_url = ("%s&%s" % (startswith_url,options)) print ("%s" % self.full_url) def APIRequestJSON(self): req = urllib.request.Request(self.full_url) try: response = urllib.request.urlopen(req).read().decode("utf-8") self.data=json.loads(response) print(self.data) except HTTPError as e: print(e.read().decode("utf-8")) return self.data def APIRequestXML(self): req = urllib.request.Request(self.full_url) try: response = urllib.request.urlopen(req).read().decode("utf-8") self.data=xmltodict.parse(response, process_namespaces=True) print(self.data) except HTTPError as e: print(e.read().decode("utf-8")) return self.data