河南高级会员
专心做事,诚信做人
posts - 14,  comments - 1,  trackbacks - 0

class tbk:
    __login_cookie = ""
    __fetch_taobaoke_url = ""
    def __init__(self):
        self.__login_cookie = self.read_file("../cookie.txt") #cookie 文件  
        self.__fetch_taobaoke_url = "http://pub.alimama.com/common/code/getAuctionCode.json?auctionid="
    #获得token        
    def _tb_token_(self):
        token = re.findall("_tb_token_=(.*)(; v=0)",self.__login_cookie)
        return token[0][0]
    def read_file(self,filename):
        all_the_text = ""
        file_object = open(filename)
        try:
            all_the_text = file_object.read( )
        finally:
            file_object.close( )
        return all_the_text

    #获取淘宝客链接
    def getTBKurl(self,**obj):
        url = self.__fetch_taobaoke_url+"%d&adzoneid=%d&siteid=%d&t=&_tb_token_=%s"%(obj['id'],obj['adzoneid'],obj['siteid'],self._tb_token_())
        result = self.fetch(url,self.__login_cookie)
        api = re.findall(r'eliteUrl":.*,"clickUrl',result)
        if len(api)>0:
            try:
                return api[0].split('\"')[2]
            except Exception,ex:
                print Exception,":",ex
                sys.exit()
        else:
            return False

#测试内容,需要加载pycurl和re模块,fetch方法没列出。fetch是pycurl请求方法。 if __name__ == "__main__": my_taobaoke = tbk() print my_taobaoke.getTBKurl(id=40094111949,siteid=4234,adzoneid=123567)

 

posted on 2015-01-05 15:43  河南高级会员  阅读(...)  评论(...编辑  收藏