Taobao official has access to merchandise promotion links API, but the API belongs to the value-added API ordinary developers do not invoke the right to apply for open
#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 Safa Ri/537.36 '}
#cookie Support
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 ' Get promotion link failed '
# Else:
# print ' Login failed '