打開登陸主頁面,得到相應的cookiecss
發送post數據包,得到響應html
根據響應返回的state查看登陸是否成功,若未成功,能夠根據返回的message信息查找對應的解決方法。python
採用IE的F12開發者工具,截獲淘寶登陸數據,從中分析驗證碼下載地址,截獲數據以下,主要是須要下面的兩個POST包,第一個POST方法是詢問是否須要驗證碼驗證,第二個POST方法是發送登陸所須要的數據包。cookie
查看第一個POST方法詳細信息,能夠查看詳細的請求參數,這裏不顯示了,響應以下:app
接着確定要獲取驗證碼,那麼咱們查看緊接着POST方法的第一個GET方法信息,看到響應以下:函數
看到的確收到了驗證碼圖片,地址如上所示,切換到DOM資源管理器,查看源網頁HTML碼以下:工具
至此,驗證碼地址已經找到了。post
要注意的是,驗證碼地址是動態生成的,每次訪問登陸界面,會得到不一樣的驗證碼,那麼咱們須要獲取對應的驗證碼地址。ui
打開一個登錄界面,就會得到一個cookie,驗證碼地址也包含在其中,所以,在程序中,咱們只須要打開一次登陸界面,從中提取出驗證碼地址便可。url
# -*- coding: utf-8 -*- import urllib import urllib2 import cookielib import re #登陸地址 tbLoginUrl = "https://login.taobao.com/member/login.jhtml" checkCodeUrl = '' #post請求頭部 headers = { 'x-requestted-with': 'XMLHttpRequest', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'ContentType': 'application/x-www-form-urlencoded; chartset=UTF-8', 'Host': 'login.taobao.com', 'DNT': 1, 'Cache-Control': 'no-cache', 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer' : 'https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Fwww.taobao.com%2F', 'Connection' : 'Keep-Alive' } #用戶名,密碼 username = "your username" password = raw_input("Please input your password of taobao: ") #請求數據包 postData = { 'TPL_username':username, 'TPL_password':password, "need_check_code" : "false", "loginsite": 0, "newlogin":1, 'TPL_redirect_url':'', 'from':'tbTop', 'fc':"default", 'style':'default', 'css_style':'', 'tid':'', 'support':'000001', 'CtrlVersion':'1,0,0,7', 'loginType':3, 'minititle':'', 'minipara' :'', "umto":"NAN", 'pstrong':2, 'llnick':'', 'sign':'', 'need_sign':'', "isIgnore":'', "full_redirect":'', 'popid':'', 'callback':'1', 'guf':'', 'not_duplite_str':'', 'need_user_id':'', 'poy':'', 'gvfdcname':10, 'from_encoding':'', "sub":'', "allp":'', 'action':'Authenticator', 'event_submit_do_login':'anything', 'longLogin':0 } #登陸主函數 def loginToTaobao(): #cookie 自動處理器 global checkCodeUrl cookiejar = cookielib.LWPCookieJar()#LWPCookieJar提供可讀寫操做的cookie文件,存儲cookie對象 cookieSupport= urllib2.HTTPCookieProcessor(cookiejar) opener = urllib2.build_opener(cookieSupport, urllib2.HTTPHandler) urllib2.install_opener(opener) #打開登錄頁面 taobao = urllib2.urlopen(tbLoginUrl) resp = taobao.read().decode("gbk") #提取驗證碼地址 pattern = r'img id="J_StandardCode_m" src="https://s.tbcdn.cn/apps/login/static/img/blank.gif" data-src="(\S*)"' checkCodeUrlList = re.findall(pattern, resp) checkCodeUrl = checkCodeUrlList[0] print "checkCodeUrl:", checkCodeUrl #此時直接發送post數據包登陸 sendPostData(tbLoginUrl, postData, headers) if checkCodeUrl != "": getCheckCode(checkCodeUrl) sendPostData(tbLoginUrl, postData, headers) def sendPostData(url, data, header): print "+"*20+"sendPostData"+"+"*20 data = urllib.urlencode(data) request = urllib2.Request(url, data, header) response = urllib2.urlopen(request) #url = response.geturl() text = response.read().decode("gbk") info = response.info() status = response.getcode() response.close() print status print info print "Response:", text result = handleResponseText(text) if result["state"]: print "successfully login in!" else: print "failed to login in, error message: ",result["message"] def handleResponseText(text): """處理登陸返回結果""" global checkCodeUrl print "+"*20+"handleResponseText"+"+"*20 text = text.replace(',', ' ') responseData = {"state": False, "message" : "", "code" : ""} m1 = re.match(r'\{?"state":(\w*)\ ', text) if m1 is not None: s = m1.group(1) if s == "true": responseData["state"] = True else: m2 = re.search(r'"message":"(\S*)"( |})', text) if m2 is not None: msg = m2.group(1) responseData["message"] = msg.decode("utf-8") else: print "failed to get the error message" m3 = re.match(r'.+\"code":(\w*)\ ', text) if m3 is not None: code = m3.group(1) responseData["code"] = code # if code == "1000": # getCheckCode(checkCodeUrl) else: print "failed to get the error code" return responseData def getCheckCode(url): print "+"*20+"getCheckCode"+"+"*20 response = urllib2.urlopen(url) status = response.getcode() picData = response.read() path = "C:\\Users\\Echo\\Desktop\\checkcode.jepg" if status == 200: localPic = open(path, "wb") localPic.write(picData) localPic.close() print "請到%s,打開驗證碼圖片"%path checkCode = raw_input("請輸入驗證碼:") print checkCode, type(checkCode) postData["TPL_checkcode"] = checkCode postData["need_check_code"] = "true" else: print "failed to get Check Code, status: ",status if __name__ == "__main__": loginToTaobao()
運行結果:
可見,成功的拿到了token,模擬登陸成功!