Python模拟登陆 —— 征服验证码 7 京东

浏览: 1670

作者:SeanCheney

來源:简书 


京东的登录表单设置了许多隐藏字段,如下所示:

所以都要获取下来。

同样也是输错三次之后出现authcode。

验证码

import requests
from bs4 import BeautifulSoup
import time

try:
input = raw_input
except:
pass

class 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 = un
self.pw = pw

def 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 authcode

def get_info(self):
'''获取登录相关参数'''
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'] = authcode
else:
data['authcode'] = ''

except Exception as e:
print (e)
finally:
return data
def login(self):

postdata = self.get_info()
postdata['loginname'] = self.un
postdata['nloginpwd'] = self.pw
postdata['loginpwd'] = self.pw
try:
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()
推荐 1
本文由 Python爱好者社区 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册