|
| 1 | +#coding=gbk |
| 2 | + |
| 3 | +from __future__ import unicode_literals |
| 4 | +import sys,re |
| 5 | +import urllib,urllib2,cookielib |
| 6 | + |
| 7 | +class TBLogin(): |
| 8 | + |
| 9 | + def __init__(self,user,pwd): |
| 10 | + #获取一个保存cookie的对象 |
| 11 | + cj = cookielib.LWPCookieJar() |
| 12 | + #将一个保存cookie对象,和一个HTTP的cookie的处理器绑定 |
| 13 | + cookie_support = urllib2.HTTPCookieProcessor(cj) |
| 14 | + #创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的URL的打开 |
| 15 | + opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) |
| 16 | + #将包含了cookie、http处理器、http的handler的资源和urllib2对象板顶在一起 |
| 17 | + urllib2.install_opener(opener) |
| 18 | + |
| 19 | + self.user,self.pwd = user,pwd |
| 20 | + |
| 21 | + def getHeaders(self): |
| 22 | + headers = { |
| 23 | + "User-Agent":"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13", |
| 24 | + #"User-Agent" = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13", |
| 25 | + "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 26 | + "Accept-Language":"zh-cn,zh;q=0.5", |
| 27 | + #"Accept-Encoding":"gzip,deflate", |
| 28 | + "Accept-Charset":"GB2312,utf-8;q=0.7,*;q=0.7", |
| 29 | + "Keep-Alive":"115", |
| 30 | + "Connection":"keep-alive" |
| 31 | + } |
| 32 | + return headers |
| 33 | + |
| 34 | + def getLoginData(self): |
| 35 | + """ |
| 36 | + 得到登陆淘宝时所需提交的数据 |
| 37 | + """ |
| 38 | + |
| 39 | + login_data = { |
| 40 | + 'ua':'', |
| 41 | + 'TPL_username':self.user.encode('gbk'), |
| 42 | + 'TPL_password':self.pwd, |
| 43 | + 'TPL_checkcode':'', |
| 44 | + 'need_check_code':'', |
| 45 | + 'longLogin':'1', #十天免登陆 |
| 46 | + 'action':'Authenticator', |
| 47 | + 'event_submit_do_login':'anything', |
| 48 | + 'TPL_redirect_url':'', |
| 49 | + 'from':'tb', |
| 50 | + 'fc':'default', |
| 51 | + 'style':'default', |
| 52 | + 'css_style':'', |
| 53 | + 'tid':'', |
| 54 | + 'support':'000001', |
| 55 | + 'CtrlVersion':'1,0,0,7', |
| 56 | + 'loginType':'3', |
| 57 | + 'minititle':'', |
| 58 | + 'minipara':'', |
| 59 | + 'umto':'', |
| 60 | + 'pstrong':'3', |
| 61 | +# 'longLogin':'-1', |
| 62 | + 'llnick':'', |
| 63 | + 'sign':'', |
| 64 | + 'need_sign':'', |
| 65 | + 'isIgnore':'', |
| 66 | + 'full_redirect':'', |
| 67 | + 'popid':'', |
| 68 | + 'callback':'', |
| 69 | + 'guf':'', |
| 70 | + 'not_duplite_str':'', |
| 71 | + 'need_user_id':'', |
| 72 | + 'poy':'', |
| 73 | + 'gvfdcname':'', |
| 74 | + 'gvfdcre':'', |
| 75 | + 'from_encoding':'' |
| 76 | + } |
| 77 | + return login_data |
| 78 | + |
| 79 | + def login(self): |
| 80 | + |
| 81 | + url = 'https://login.taobao.com/member/login.jhtml' |
| 82 | + source = self.request(url,self.getLoginData()) |
| 83 | + if source: |
| 84 | + print source |
| 85 | + print self.checkLoginError(source) |
| 86 | + |
| 87 | + def checkLoginError(self,source): |
| 88 | + |
| 89 | + needVerifyCodeError = re.compile('<div id="J_Message" class="login-msg msg">.*<p class="error">(.*?)</div>',re.S) |
| 90 | + r = needVerifyCodeError.search(source) |
| 91 | + return r.group(1) if r else None |
| 92 | + |
| 93 | + def request(self,url,postData=dict()): |
| 94 | + |
| 95 | + postData = urllib.urlencode(postData) if postData else None |
| 96 | + header = self.getHeaders() |
| 97 | + |
| 98 | + req = urllib2.Request( |
| 99 | + url = url, |
| 100 | + data = postData, |
| 101 | + headers = header |
| 102 | + ) |
| 103 | + try: |
| 104 | + request = urllib2.urlopen(req) |
| 105 | + source = request.read() |
| 106 | + # print request.code,request.msg |
| 107 | + request.close() |
| 108 | + return source |
| 109 | + except: |
| 110 | + info=sys.exc_info() |
| 111 | + print info[0],":",info[1] |
| 112 | + return None |
| 113 | + |
| 114 | +if __name__ == '__main__': |
0 commit comments