您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 阿里分类信息网,免费分类信息发布

python模拟登陆阿里妈妈生成商品推广链接

2024/3/23 3:16:37发布46次查看
淘宝官方有获取商品推广链接的api,但该api属于增值api 普通开发者没有调用权限 需要申请开通
备注:登陆采用的是阿里妈妈账号登陆非淘宝账号登陆
代码如下:
#coding:utf-8
__author__ = 'liukoo'
import urllib,urllib2,cookielib,re
from hashlib import md5
class alimama:
    def __init__(self):
        self.header = {'user-agent':'mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.95 safari/537.36'}
        #cookie 支持
        self.cookie_handle = cookielib.cookiejar()
        self.opener = urllib2.build_opener(urllib2.httpcookieprocessor(self.cookie_handle))
        urllib2.install_opener(self.opener)
    #登陆
    def login(self,username,passwd):
        login_data = {
            'logname':'',
            'originallogpasswd':'',
            'logpasswd':'',
            'proxy':'',
            'redirect':'',
            'style':''
        }
        login_data['logname'] =username
        login_data['originallogpasswd'] =passwd
        login_data['logpasswd'] = md5(login_data['originallogpasswd']).hexdigest()
        source = urllib2.urlopen('http://www.alimama.com/member/minilogin.htm').read()
        token_list = re.findall(rinput name='_tb_token_' type='hidden' value='([a-za-z0-9]+)', source)
        login_data['_tb_token_'] = token_list[0] if token_list else ''
        loginurl = 'https://www.alimama.com/member/minilogin_act.htm'
        #拼接post数据
        login_data = urllib.urlencode(login_data)
        self.header['referer'] = 'http://www.alimama.com/member/minilogin.htm'
        try:
            req = urllib2.request(url=loginurl,data=login_data,headers=self.header)
            resp =urllib2.urlopen(req)
            html = resp.read()
            if str(resp.url).find('success')!=-1:
                return true
        except exception,e:
            print e
            return false
    #获取商品的推广链接
    def geturl(self,url):
        try:
            item_id = re.search(rid=(\d+),url)
            item_id = item_id.group(1)
            html = urllib2.urlopen('http://u.alimama.com/union/spread/common/allcode.htm?specialtype=item&auction_id='+item_id).read()
            rule = re.compile(rvar clickurl = \'([^\']+))
            return rule.search(html).group(1)
        except exception,e:
            print e
            return false
#example
# ali = alimama()
# if ali.login('admin@liuko.com','xxxxxx'):
#     url = ali.geturl('http://item.taobao.com/item.htm?spm=a1z10.1.w4004-1205618817.6.evkf6o&id=19322457214')
#     if url:
#         print url
#     else:
#         print '获取推广链接失败'
# else:
#     print '登陆失败'
阿里分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录