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

yipeiwu_com5年前Python基础

淘宝官方有获取商品推广链接的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(r"input 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(r"id=(\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(r"var 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 '登陆失败'

相关文章

Python的另外几种语言实现

Python自身作为一门编程语言,它有多种实现。这里的实现指的是符合Python语言规范的Python解释程序以及标准库等。这些实现虽然实现的是同一种语言,但是彼此之间,特别是与CPyt...

tensorflow实现在函数中用tf.Print输出中间值

tensorflow实现在函数中用tf.Print输出中间值

tensorflow由于其基于静态图的模式,导致写代码的时候很难调试,除了用官方的调试工具外,最直接的方法就是把中间结果输出出来查看,然而,直接用print函数只能输出tensor变量的...

Python3 虚拟开发环境搭建过程(图文详解)

Python3 虚拟开发环境搭建过程(图文详解)

虚拟环境的搭建 为什么要使用虚拟环境# 1、使不同应用开发环境相互独立 2、环境升级不影响其他应用,也不会影响全局的python环境 3、防止出现包管理混乱及包版本冲突 windows平...

python创建线程示例

复制代码 代码如下:import threadingfrom time import sleep def test_func(id):    for i i...

Python字典的基本用法实例分析【创建、增加、获取、修改、删除】

本文实例讲述了Python字典的基本用法。分享给大家供大家参考,具体如下: 字典是一系列的键值对 。 每个键都与一个值相关联, 我们可以使用键来访问与之相关联的值。 与键相关联的值可以任...