淘宝api自己的封装之python版 2023-01-31 05:01:00 自己的 封装 淘宝 #!/usr/bin/env python import datetime try : import JSON except ImportError : import simplejson as json import urllib, hashlib import base64 ENVIRONMENT = 'devel' # or product if ENVIRONMENT == 'devel' : AppKey = 'test' AppSecret = 'test' Gateway = 'Http://gw.api.tbsandbox.com/router/rest' elif ENVIRONMENT == 'product' : AppKey = '12345678' AppSecret = '------------------------------------' Gateway = 'http://gw.api.taobao.com/router/rest' else : import sys print >>sys.stderr, "ENVIRONMENT is neither devel nor product" sys.exit(1) FORMat = 'json' SignMethod = 'md5' APIVersion = '2.0' SDKVersion = 'tao_api_Python_1.0' class Client : def __init__(self, **kwargs) : self.sys_params = { 'app_key' : AppKey, 'v' : APIVersion, 'format' : Format, 'sign_method' : SignMethod, 'partner_id' : SDKVersion } self.app_secret = AppSecret self.gateway = Gateway if kwargs : self.sys_params.update(kwargs) def sign(self, params) : items = params.items() items.sort() s = self.app_secret for i in items : s += '%s%s' % i s += self.app_secret m = hashlib.md5() m.update(s) return m.hexdigest().upper() def execute(self, request, session = None) : d = self.sys_params.copy() api_params = request.get_api_params() d['method'] = request.get_method_name() d['timestamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") if session is not None : d['session'] = session api_params.update(d) d['sign'] = self.sign(api_params) param_string = urllib.urlencode(d) url = '%s?%s' % (self.gateway, param_string) data_string = urllib.urlencode(api_params) try : http = urllib.urlopen(url, data_string) resp = json.load(http) http.close() values = resp.values() return values[0] if values else None except : return None class TopRequest : def __init__(self, method_name) : self.method_name = method_name self.api_params = {} def get_api_params(self) : return self.api_params def get_method_name(self) : return self.method_name def __setitem__(self, param_name, param_value) : self.api_params[param_name] = param_value def decode_top_parameters(top_parameters) : params = {} param_string = base64.b64decode(top_parameters) for p in param_string.split('&') : key, value = p.split('=') params[key] = value return params if __name__ == '__main__' : req = TopRequest('taobao.items.get') req['nicks'] = 'sandbox_c_1' req['fields'] = 'num_iid,title,nick,pic_url,cid,price' req['q'] = '' client = Client() j = client.execute(req) # use session = 'blablablablablalbah' when session is needed print j['items']['item'][0]['title'].encode('utf8') 要使用此代码,需要安装json模块或者simplejson模块之一,json模块为python2.6自带,对于之前版本的python,可以下载安装simplejson。代码中没有对两个模块都没有安装的测试。 python版与PHP版之结构和实现方式是相同的,如何使用,可以查看 if __name__ == '__main__': 之后的代码 相关文章