Python模拟登陆 —— 征服验证码 7 京东
生活随笔
收集整理的這篇文章主要介紹了
Python模拟登陆 —— 征服验证码 7 京东
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
登錄界面
京東的登錄表單設(shè)置了許多隱藏字段,如下所示:
隱藏字段所以都要獲取下來。
同樣也是輸錯三次之后出現(xiàn)authcode。
驗證碼 import requests from bs4 import BeautifulSoup import timetry:input = raw_input except:passclass JDlogin(object):def __init__(self,un,pw):self.headers = {'User-Agent':"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding':'gzip, deflate, sdch','Accept-Language':'zh-CN,zh;q=0.8','Connection':'keep-alive',}self.session = requests.session()self.login_url = "http://passport.jd.com/uc/login"self.post_url = "http://passport.jd.com/uc/loginService"self.auth_url = "https://passport.jd.com/uc/showAuthCode"self.un = unself.pw = pwdef get_authcode(self,url):self.headers['Host'] = 'authcode.jd.com'self.headers['Referer'] = 'https://passport.jd.com/uc/login'response = self.session.get(url, headers = self.headers)with open('authcode.jpg','wb') as f:f.write(response.content)authcode = input("plz enter authcode:")return authcodedef get_info(self):'''獲取登錄相關(guān)參數(shù)'''try:page = self.session.get(self.login_url, headers = self.headers )soup = BeautifulSoup(page.text,"lxml")input_list = soup.select('.form input')data = {}data['uuid'] = input_list[0]['value']data['eid'] = input_list[4]['value']data['fp'] = input_list[5]['value']data['_t'] = input_list[6]['value']rstr = input_list[7]['name']data[rstr] = input_list[7]['value']acRequired = self.session.post(self.auth_url, data={'loginName':self.un}).text #返回({"verifycode":true})或({"verifycode":false})if 'true' in acRequired:print ('need authcode, plz find it and fill in ')acUrl = soup.select('.form img')[0]['src2']acUrl = 'http:{}&yys={}'.format(acUrl,str(int(time.time()*1000)))authcode = self.get_authcode(acUrl)data['authcode'] = authcodeelse:data['authcode'] = ''except Exception as e:print (e)finally:return datadef login(self):postdata = self.get_info()postdata['loginname'] = self.unpostdata['nloginpwd'] = self.pwpostdata['loginpwd'] = self.pwtry:self.headers['Host'] = 'passport.jd.com'self.headers['Origin'] = 'https://passport.jd.com'self.headers['X-Requested-With'] = 'XMLHttpRequest'login_page = self.session.post(self.post_url, data = postdata, headers = self.headers)print (login_page.text) #若返回{“success”:”http://www.jd.com”},說明登錄成功except Exception as e:print (e)if __name__=="__main__":username = input("plz enter username:")password = input("plz enter password:")JD = JDlogin(username,password)JD.login()總結(jié)
以上是生活随笔為你收集整理的Python模拟登陆 —— 征服验证码 7 京东的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mblu是什么牌子的手机
- 下一篇: AMD 发布芯片组驱动程序版本 5.01